Decode string in hexadecimal notation

In R, which is an efficient way of converting a string encoded in hexadecimal form, for example "40414243"

to its equivalent characters, for example. "@ABC"

?

For example, the equivalent of this code:

library(stringr)

FromHexString <- function (hex.string) {
  result <- ""
  length <- str_length(hex.string)
  for (i in seq(1, length, by=2)) {
    hex.value <- str_sub(hex.string, i, i + 1)
    char.code <- strtoi(hex.value, 16)
    char <- rawToChar(as.raw(char.code))
    result <- paste(result, char, sep="")
    char
  }
  result
}

      

What produces:

> FromHexString("40414243")
[1] "@ABC"

      

While the above code works, it is inefficient at all using a lot of string concatenations.

So the question is how to write an idiomatic, efficient R function that does this operation .

Edit: my sample only works for ASCII encoding, not UTF-8 encoded byte arrays.

+3


source to share


3 answers


Check if this is more efficient (for longer lines):

string <- "40414243"

intToUtf8(
  strtoi(
    do.call(
      paste0, 
      as.data.frame(
        matrix(
          strsplit(string, split = "")[[1]], 
          ncol=2, 
          byrow=TRUE), 
        stringsAsFactors=FALSE)), 
    base=16L)
)
#[1] "@ABC"

      



Otherwise, you can look for a C / C ++ implementation.

+4


source


Modify your code to use lookup tables R example here . Your lookup table will have 255 values. Place them in a vector and get their values ​​from that vector.



No: no other solution will beat this if you need to make a lot of conversions.

+1


source


If you don't want to use a lookup table (or just like codegolfing :-)), consider writing a vectorized version of something like:

bar <- unlist(strsplit(foo,'')) #separates input into individual elements
items <- sapply(1:(length(bar)/2),function(j)paste0(bar[(2*j-1):(2*j)],sep='',collapse=''))

      

followed by strtoi

or something else.

But even easier (hopefully ...) is

sapply(1:(nchar(foo)/2) function(j) substr(foo,(2*j-1),(2*j)))

      

+1


source







All Articles