Converting hex format to float numbers in R

夙愿已清 提交于 2020-12-04 03:19:42

问题


could you please someone help me how we can convert 4 byte hex format to float number in R? for example I want to transfer "aec7a042" to 80.39 . I could not find anything in R after lots of search to give me this conversion! The C function is BitConverter. ToSingle. But I need to do the same in R? Can anyone help me, please?


回答1:


You can read this value using readBin. It seems you have a 4-byte signed float value. You can read that with:

readBin("aec7a042", "double", size=4)
# [1] 80.39

If this doesn't work in your version of R, try

x <- "aec7a042"
readBin(as.raw(strtoi(substring(x, (step<-seq(1, nchar(x), by=2)), step+1), 16)), "double",n=1,size=4)
# or 
readBin(as.raw(strtoi(apply(matrix(strsplit(x,"")[[1]],2),2,paste, collapse=""), 16)), "double", size=4)

Here we more explicitly convert the character string to a raw vector of bytes.



来源:https://stackoverflow.com/questions/39461349/converting-hex-format-to-float-numbers-in-r

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