Reading and decoding binary data

prothero at earthednet.org prothero at earthednet.org
Wed Mar 12 19:55:44 EDT 2014


Folks:
Before I bang my head against the wall too much, I'd like to make sure I'm approaching this task correctly.
I have a digital elevation file consisting of 16 bit 2's complement data. I first determine the position of the data for the desired location, then I do a binary read. 

  put getAFilePath("files/dems/") into fPath
   put fPath&"ETOPO5.DAT" into theFile
   open file theFile for binary read
   if the result <> empty then
      answer "Couldn't open DEM file"
      exit readDemTest
   end if

--Then i calculate the byte position where the data should be, then read it using:
      read from file theFile at thePos for 8  --read 8 chars beginning at thePos (I should only need 2 to get the value)
      put it into theDepths
      put byteToNum(byte 1 of theDepths) into b1
      put byteToNum(byte 2 of theDepths) into b2

--Then, because I don't remember the byte order, I'm trying each one to see which gives me sensible results
--It should have a sign bit, so only 7 bits of the most significant byte should count, and the first one is the sign bit.

      if b1 > 127 then  --sign is negative. numbers from 128 to 255 should be negative
         put -256+b1 into msb
         put 256*msb - b2 into depth1
      else  --sign is positive
         put b1 into msb
         put 256*msb + b2 into depth1
      end if
--try the other way
      if b2 > 127 then
         put -256+b2 into msb
         put 256*msb - b1 into depth2
      else
         put b2 into msb
         put 256*msb + b1 into depth2
      end if

What I'm wondering before I spend more time on this, is whether the livecode commands are the correct ones for this. I have old C code that does some of the same processing, but I can't run it now to test.

Thanks,
Bill

William Prothero
http://es.earthednet.org






More information about the use-livecode mailing list