Endian of reinterpret(T, array)


#1

Hi All,

I was received an array from a file as:

julia> a1 = UInt8[0x0, 0x32, 0, 0x33, 0, 0x34, 0, 0x35,0,0]
10-element Array{UInt8,1}:
 0x00
 0x32
 0x00
 0x33
 0x00
 0x34
 0x00
 0x35
 0x00
 0x00

julia> a2 = reinterpret(UInt16, a1) # See the order flip here of the bytes
5-element Array{UInt16,1}:
 0x3200
 0x3300
 0x3400
 0x3500
 0x0000

julia> transcode(String, a2)
"㈀㌀㐀㔀\0"

Is Julia little endian by default or the behavior will depend on the processor type?

regards,

Sambit


#2

reinterpret is direct memory cast so it’s always native endian (and in practice always little endian)…


#3

Thanks @yuyichao

I think I got what I wanted. I will need to write the code by first querying Base.ENDIAN_BOM and then see if nto* methods can be used.