问题
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