Convert hex encoded string to byte array The following should read a hex string such as "ece998a2fb" into vector 236 233 152 162 251. I was unable to find any built in method for doing this.

as.byte.vector &lt;- function(hex.string) {
  vapply(seq(to=nchar(hex.string), by=2), function(idx) {
    as.numeric(paste("0x", substr(hex.string, idx, idx+1), sep=''))
  }, FUN.VALUE = double(1))
}
One improvement is you can use the vectorized function substring rather than substr. This way, you can precompute all hexadecimal pairs and feed them directly into paste and as.numeric which are also vectorized. This removes the need for vapply (a disguised and slowish for loop).

You could also use paste0 as a shortcut for paste(, sep = ""). paste0 has been around for what seems like a couple years. If you are concerned about compatibility with older versions of R, then yes, it is safer to stick with paste.

I could be wrong but I see no reason not to output a vector of integers instead of numerics. That integers use less memory and are not subject to floating point errors are two strong reasons to prefer them over numerics.

It is also a good idea to add input checks to test your assumptions. Here it seems that the input must be a character vector of length 1.

This comment is more subjective. You have crammed a lot of operations into a single statement. I can count 8 functions including the anonymous one you created. That really makes your code hard to understand.

In all, I would have written the function like this:

as.byte.vector &lt;- function(hex.string) {
   stopifnot(is.character(hex.string), length(hex.string) == 1L)
   start.idx &lt;- seq(from = 1L, to = nchar(hex.string), by = 2L)
   end.idx &lt;- start.idx + 1L
   hex.pairs &lt;- substring(hex.string, start.idx, end.idx)
   prefixed.pairs &lt;- paste0("0x", hex.pairs)
   as.integer(prefixed.pairs)
}


I hope you will agree that it is easier to read when the code is broken out this way, with each statement using a very few functions, and where the names of the variables holding intermediate results help understand the process.

This should already be faster than your version, thanks to the replacement of vapply. If speed is a real concern, you could make significant improvement by replacing substring with the following regular expression trick:

hex.pairs &lt;- strsplit(gsub("(..)", "\\1 ", hex.string), " ")[[1]]


(We insert a space every two characters, then split on white space.)