[futurebasic] Re: Mac Number conversion (LSB/MSB)

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 1997 : Group Archive : Group : All Groups

From: Greg_Neagle@...
Date: Mon, 08 Dec 1997 17:53:39 -0800
On 12/7/97 at 9:40 PM, Mel Patrick wrote:

:I am reading a file which has binary numbers stored in LSB/MSB format so
:some bytes read in will look like this :
:
:73 0 0 0 27 01 0 0
:
:Those are basically TWO longs. The first is 73 0 0 0 (LSB->MSB) and then 27
:01 0 0 is the second one. If memory serves right, the Mac stores in MSB/LSB.
:
:I just bet someone has a real fast efficient way of turing those digits
:around (they are in binary in the file). If I was still in Z80, I'd do a
:rotate thru the carry. I'm not.
:
:But I have a lot of processing to do with longs and words stored in this
:format.

How strange.  I just started work on a project today that also reads file with
its numbers stored in LSB/MSB format (little-endian?).  I decided to do the
conversion while reading the file, like so:

LOCAL FN readShort(fileNum)
  DIM 2 a$
  DIM x
  READ# fileNum, a$;2
  x = PEEK(@a$+1) + (PEEK(@a$+2) << 8)
END FN = x

LOCAL FN readLong&(fileNum)
  DIM 4 a$
  DIM x&
  READ# fileNum, a$;4
  x& = PEEK(@a$+1) + (PEEK(@a$+2) << 8) + (PEEK(@a$+3) << 16) + (PEEK(@a$+4) <<
24)
END FN = x&

Call these like:

aShort% = FN readShort(fileNum)
aLong& = FN readLong&(fileNum)

I'm sure there is a faster way, though I can't imagine that this is too bad.


- via BulkRate 2.6.x