[REBOL] Re: Unusual file format
From: larry:ecotope at: 14-Nov-2000 14:38
Hi Mike
The tricky part is creating the 2-byte integers. Here is a useful function
to convert REBOL integers to 2-byte or 4-byte byte-reversed (Intel format)
binary values:
=====TO-BYTE FUNCTION======
to-byte: func [int [integer!] /long][
head reverse do join "#{" [(copy skip (to-string to-hex int) either
long [0][4]) "}"]
]
Example:
>> bin: to-byte 694
== #{B602}
Note: there is no checking for sign, so TO-BYTE will only work properly for
unsigned 2-byte up to 32767 and unsigned 4-byte up to 2147483647. It is
fairly easy to add code to allow REBOL negative integers to be
re-interpreted as the high-half unsigned's.
The field length is just one byte and can be generated as:
binary-byte: to-binary to-string to-char <integer between 0 and 255>
So, in outline, for each line to be written:
1) Convert the string content to binary
b-string: to-binary <data-string>
2) insert b-string binary-byte ; binary-byte as above
3) insert b-string to-byte <field number as integer between 0 and 32767>
4) write/binary/append <filename> b-string ; append the line to the file.
5) After all lines are written
write/binary/append <filename> #{FFFF}
HTH
-Larry