[Q][Smalltalk][hex] converting binary to hex
Ned Konz
ned at bike-nomad.com
Tue Mar 19 23:09:23 UTC 2002
On Tuesday 19 March 2002 02:46 pm, Ken G. Brown wrote:
> Attached is a ByteArray method 'asHex' change set which I am presenting for
> Tersemen everywhere to critically review before I post it with the [ENH].
> It returns a ByteArray containing all the two character representations of
> all bytes in the original ByteArray.
>
> aByteArray _ #(0 15 16 255) asByteArray.
> aByteArray asHex. answers - a ByteArray(48 48 48 70 49 48 70 70)
>
> Any and all comments will be appreciated.
> Thx to all who helped.
> Ken
This is about 35% faster and generates a bit less garbage (I think):
asHex
"return a ByteArray containing the hex representation of the original
ByteArray"
| hex retval |
hex _ '0123456789ABCDEF'.
retval _ ByteArray new: self size * 2.
self with: (1 to: self size * 2 by: 2) do: [ :each :i |
retval at: i put: (hex at: each // 16 + 1) asciiValue.
retval at: i + 1 put: (hex at: each \\ 16 + 1) asciiValue
].
^retval
--
Ned Konz
currently: Stanwood, WA
email: ned at bike-nomad.com
homepage: http://bike-nomad.com
More information about the Squeak-dev
mailing list
|