converting binary to hex

hannes.hirzel.squeaklist at bluewin.ch hannes.hirzel.squeaklist at bluewin.ch
Tue Mar 19 05:34:58 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.
> #(10 15 16) would end up as #('0A' '0F' '10')
> I figure it must be in there somewhwere but I cannot find it.
> 
> 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:
> 10 hex. '16rA'
> 16 hex. '16r10'
> 
I did not check thouroughly the image so others might come up with a
better solution.

However the following code snippet does the job 

| h res |
#(10 15 16 )
		collect: [:elem | 
			h _ elem hex.
			res _ h last asString.
			h _ h allButLast.
			h last = $r
				ifTrue: ['0' , res]
				ifFalse: [h last asString , res].
			]

In any case it takes just the last two hex digits. It is not a general
solution.


Regards
Hannes Hirzel

P.S. May I ask you to put in some title tags the next time you send a
mail
see http://minnow.cc.gatech.edu/squeak/1962
(Title Tags on the Squeak Email List)

I just changed to the Squeak email reader "Celeste". It has the  feature
to look at the mail with filters. There it comes in handy if the emails
have tags. And tags are used in other contexts as well. Think of the
mailing list as beeing an ethernet connection where various packets are
sent. They have to have addresses and tags so that the  parties can grab
out the packets they are interested in.

In this case they could be 

[Q][Smalltalk][hex] converting a ByteArray to hex

interpretation 
- question
- Smalltalk related 
- free keyword: 'hex'



More information about the Squeak-dev mailing list