Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a hex string to text in R?

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?

like image 513
freiste1 Avatar asked Mar 25 '15 09:03

freiste1


2 Answers

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"
like image 55
jbaums Avatar answered Oct 26 '22 12:10

jbaums


Just to add to @jbaums answer or to simplify it

library(wkb)

hex_string <- '231458716E234987'

hex_raw <- wkb::hex2raw(hex_string)

text <- rawToChar(as.raw(strtoi(hex_raw, 16L)))
like image 1
iamigham Avatar answered Oct 26 '22 13:10

iamigham