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?
Asked
Active
Viewed 1,613 times
5

Pierre L
- 28,203
- 6
- 47
- 69

Parisa Kouchaki
- 51
- 3
-
Can you explain further how your hex digits equal `80.39`? – Pierre L Sep 13 '16 at 02:53
-
Actually the function you mentioned is not a C function, I think you can find a workaround to read binary data, this might be helpful: http://www.ats.ucla.edu/stat/r/faq/read_binary.htm – DarioDF Sep 13 '16 at 02:53
-
@PierreLafortune - it does seem to fit - e.g. - https://gregstoll.dyndns.org/~gregstoll/floattohex/ - returns the same result, albeit reversed. – thelatemail Sep 13 '16 at 03:18
-
@thelatemail I get `0x42a0c7ae`. It is close to OP's but far enough to require clarity – Pierre L Sep 13 '16 at 03:21
-
@MrFlick Thank you so much for your help. readBin did not work but the other two codes work properly, I really appreciate that. – Parisa Kouchaki Sep 13 '16 at 04:47
1 Answers
6
Formerly in R you used to be able to read this value directly with readBin
. It seems you have a 4-byte signed float value. You can read that with:
readBin("aec7a042", "double", size=4)
# [1] 80.39
However newer versions of R require just a bit more work. These alternatives should work
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)
# or
readBin(as.raw(strtoi(strsplit(x,"(?<=..)",p=TRUE)[[1]],16)), "double", size=4)
The idea is that we need to split the string into two character chunks and then convert those chunks to integer values and treat chose values like bytes. That part looks like
xraw <- strsplit(x,"(?<=..)",p=TRUE)[[1]] |> # [1] "ae" "c7" "a0" "42"
strtoi(16) |> # [1] 174 199 160 66
as.raw() # [1] ae c7 a0 42
Here we more explicitly convert the character string to a raw vector of bytes. So you see xraw
contains the same data, but it's not in character form any more, it's an actual "raw" binary vector. And that's what readBin was expecting
readBin(xraw, "double", size=4)
# [1] 80.39

MrFlick
- 195,160
- 17
- 277
- 295
-
Has `readBin` been updated recently? That does not work on R 3.2.4, with an error about the input needing to be binary. – thelatemail Sep 13 '16 at 04:23
-
1A more verbose version would be `x<-"aec7a042"; readBin(as.raw(strtoi(substring(x, (step<-seq(1, nchar(x), by=2)), step+1), 16)), "double",n=1,size=4)` using code from http://stackoverflow.com/questions/29251934/how-to-convert-a-hex-string-to-text-in-r – MrFlick Sep 13 '16 at 04:26
-
1`readBin(as.raw(strtoi(strsplit(x,"(?<=..)",p=TRUE)[[1]],16)), "double", size=4)` if you want to get fancy. – thelatemail Sep 13 '16 at 04:40
-