converting binary to hex
Richard A. O'Keefe
ok at cs.otago.ac.nz
Tue Mar 19 04:23:00 UTC 2002
"Ken G. Brown" <kbrown at tnc.ab.ca> wrote:
Where would I find a routine to convert a ByteArray from binary to hex?
ie.[sic.]
#(10 15 16) would end up as #('0A' '0F' '10')
I figure it must be in there somewhwere but I cannot find it.
Try
hex := '01234567789ABCDEF'.
aByteArray collect: [:each|
hi := each // 16 + 1.
lo := each \\ 16 + 1.
(hex copyFrom: hi to: hi), (hex copyFrom: lo to: lo)]
The intent is to convert a stream of bytes going to a file into
their hex equivalent without the 16r stuff like when you evaluate:
In that case, let's not waste time and space making a string.
hex := '0123456789ABCDEF'.
aByteArray do: [:each |
aStream nextPut: (hex at: each // 256 + 1).
aStream nextPut: (hex at: each \\ 256 + 1)].
More information about the Squeak-dev
mailing list
|