如何在 Julia 中将字节转换为浮点/双精度

vvo*_*dcx 3 julia

我有以下双字节字节:

julia> sample
8-element Array{UInt8,1}:
 0xc0
 0x54
 0x5a
 0x88
 0x26
 0x76
 0xd3
 0xd1
Run Code Online (Sandbox Code Playgroud)

我知道这个 double 的值是 -81.41456,并且可以在诸如this one 之类的网站中进行检查。但是,像下面这样的转换不起作用:

julia> reinterpret(Float64, sample)
1-element reinterpret(Float64, ::Array{UInt8,1}):
 -1.5122920043530113e86
Run Code Online (Sandbox Code Playgroud)

我可以知道将字节数组转换为浮点数或双精度数的正确方法吗?谢谢!

小智 5

问题在于构造 Float64 的顺序:

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

因此,您只需运行reinterpret(Float64,reverse(sample)),它就会起作用。