[plt-scheme] I hate to bother the list with such a rudimentary question... but!

From: Jim Blandy (jimb at red-bean.com)
Date: Tue Jan 3 19:01:53 EST 2006

On 1/3/06, geb a <geb_a at yahoo.com> wrote:
> I'm trying to read/write hexadecimal code to files.
> The problem I am having is that extra characters are
> always being added.  I have tried a number of methods
> and constantly end up with either extra characters or
> the wrong characters.
> (define test #x4d )

That definition is exactly equivalent to the code:

  (define test 77)

There's nothing about the variable 'test' or its value that remembers
that it was written in hexidecimal in the source code; it's just a
number.  You have to indicate how you want it written when you write
it.  Since 'write' normally writes numbers in decimal, it's writing
the two  characters "77" to your file.

I'm worried I may not understand what you mean by "writ[ing]
hexidecimal code to files".  Do you want the single byte whose value
is 77 in the output?  Or do you want the two ASCII characters "4d" in
the output?

If the former, use read-byte and write-byte.  If the latter, use
(write (number->string test 16)), and custom parsing code, probably
using read-byte to get each character of the hexidecimal number.

There may be some parameter that tells 'write' to use hex by default,
but I don't know what it is.

Posted on the users mailing list.