How to convert a hex string to text in R?

走远了吗. 提交于 2019-12-05 20:41:08

问题


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"


来源:https://stackoverflow.com/questions/29251934/how-to-convert-a-hex-string-to-text-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!