How to convert a hex string to text in R?

2019-04-07 16:27发布

问题:

Is there a function which converts a hex string to text in R?

For example:

I've the hex string 1271763355662E324375203137 which should be converted to qv3Uf.2Cu 17.

Does someone know a good solution in R?

回答1:

Here's one way:

s <- '1271763355662E324375203137'
h <- sapply(seq(1, nchar(s), by=2), function(x) substr(s, x, x+1))
rawToChar(as.raw(strtoi(h, 16L)))

## [1] "\022qv3Uf.2Cu 17"

And if you want, you can sub out non-printable characters as follows:

gsub('[^[:print:]]+', '', rawToChar(as.raw(strtoi(h, 16L))))

## [1] "qv3Uf.2Cu 17"