Reputation: 81
I have an array of bytes (char1) and I have to go through converting them to specific data types. For example the first two bytes in the array need to be converted to ascii characters so I just cast them using
c = string(char1[0])
but for char1[2] and char1[3] I need a 16bit unsigned integer so how would I go about combining those two bytes and casting them as uint? I'm looking for a general answer as I will need to convert to types ranging from 1 byte up to 8 bytes.
Thanks
Upvotes: 2
Views: 1623
Reputation: 641
The reason i = uint(char1[2] + ishft (char1[5], 8))
isn't working is the variable being shifted is byte and it overflows when shifted by 8. Instead convert to uint before doing the shift:
i = uint(char1[2]) + ishft(uint(char1[3]),8)
Upvotes: 1
Reputation: 2386
uint
is the routine to use. Try:
IDL> b = bindgen(2) + 1B
IDL> print, b
1 2
IDL> ui = uint(b[0:1], 0)
IDL> print, ui
513
IDL> print, 2^9 + 2^0
513
Upvotes: 2