2

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

Bogdanovist
  • 1,498
  • 2
  • 11
  • 20
user1026561
  • 81
  • 1
  • 3
  • I did some more searching and found the answer to similar questions where they said to use bitshifting, so I just tried `i = uint(char1[2] + ishft (char1[5], 8))` but it isn't working, I also tried flipping char1[2] and char1[3] – user1026561 Dec 13 '12 at 19:54

2 Answers2

2

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
mgalloy
  • 2,356
  • 1
  • 12
  • 10
1

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)
shouston
  • 641
  • 4
  • 7