DISK I/O
Swap two bytes in a file
I am reading a file version number from a PC file. I know that the version number is stored as the first 2 bytes in the file, and that because it is a PC file the bytes are in reverse order. I've been using:
PFMhandle& = FN NEWHANDLE(fileSize&)
vString$ = RIGHT$(HEX$(PEEK([PFMhandle&] + 1)),2) ' first get the second byte
vString$ = "&H" + vString$ + RIGHT$(HEX$(PEEK([PFMhandle&])),2)' put it before the first byte
Version% = VAL(vString$)' convert string to value
..to reverse the bytes into the Mac order (right to left), then get the version number.
This works fine, but I know there has got to be a more efficient way to do this.
I think this should work:
LOCAL FN swap2Bytes(adr&)
i=PEEK WORD(adr&)
i = PEEK(@i) + (PEEK(@i+1) << 8)
END FN = i
'---------
Version% = FN swap2Bytes([PFMhandle&])
Try:
Version% = PEEK([PFMhandle&]) + (PEEK ([PFMhandle&] + 1) << 8)
|