Converting an Array of Byte to Array{T}

Hello,

I have binary data read from SQLite database in the form of Array{Uint8}.

What would be the best solution to convert this binary data into an Array{T} (convert function perform element wise conversion and thus do not work).

For example if I have :

A = [0x00 0x00 0x00 0x01 0x00 0x00 0x01 0x00] 
B = bufferConvert{Int32}(A) # How to implement buffer convert ?
# B is now equal to [1  256]

You can use reinterpret to treat your array of bytes as an array of some other bitstype:

julia> reinterpret(Int32, vec(A))
2-element reinterpret(Int32, ::Array{UInt8,1}):
 16777216
    65536

Note that I had to use vec because your A is actually a 1x8 matrix, not a vector. Storing A as a vector (using commas instead of spaces as delimiters) would remove that issue.

Note also that this gives the wrong answer, presumably because you are expecting a different endianness. Using ntoh fixes that:

julia> ntoh.(reinterpret(Int32, vec(A)))
2-element Array{Int32,1}:
   1
 256
2 Likes

Ah it was what I was looking for indeed !

Yes, my examplle was just to illustrate what I meant. typeof(input) is Array{Uint8, 1}.