How to convert bytes to float/double in Julia

Viewed 331

I have the following double in bytes:

julia> sample
8-element Array{UInt8,1}:
 0xc0
 0x54
 0x5a
 0x88
 0x26
 0x76
 0xd3
 0xd1

I know the value of this double which is -81.41456, and it can be checked in websites such as this one. However, conversions like the following does not work:

julia> reinterpret(Float64, sample)
1-element reinterpret(Float64, ::Array{UInt8,1}):
 -1.5122920043530113e86

May I know the proper way of converting byte array into floats or double? Thanks!

1 Answers

The problem is the order in which the Float64 is constructed:

x=UInt8[0xd1, 0xd3, 0x76, 0x26, 0x88, 0x5a, 0x54, 0xc0];
reinterpret(Float64,x)
1-element reinterpret(Float64, ::Array{UInt8,1}):
    -81.41455995182265

So, you can just run reinterpret(Float64,reverse(sample)), and it will work.

Related