Page 1 of 1

writing a hex value to file

Posted: Fri Oct 25, 2013 11:36 am
by paulus123
Hi,
I have a hex value as string and want to write it to a file.
the hex number is F088 for example
this should be $F0 and $88 in the file and take two bytes of space.
(hi and low byte)

Re: writing a hex value to file

Posted: Fri Oct 25, 2013 12:18 pm
by burger2227
HEXadecimal values use 2 characters to express one byte of information.

That would require 4 bytes to express it as a HEX$ value, but you could convert each two characters into an ASCII byte value ranging from 0 to 255 for 00 to FF.

So if we have the HEX$ text values we can use VAL with "&H" prefix to get the byte value:

hexa$ = "F0"
byte% = VAL("&H" + hexa$)

We can then PRINT # or PUT # the value into a file as an ASCII character:

ascii$ = CHR$(byte%) '0 to 255 only
PUT #1, , ascii$

If you use QB64 you can put each value into a file AS _UNSIGNED _BYTE.