View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Fall2001 Midterm Review: String to Hex

Link back to Fall 2001 Midterm Review

Can somebody critic this code I wrote for the stringToHex method

stringToHex: someString
someTokens := (someString findTokens: ' ').
index := 1.
size := (someTokens size).
returnTokens := OrderedCollection new.
[index = size]whileTrue: [
temp := (((someTokens at: index) asNumber) hex) .
returnTokens add: temp.
index := index + 1.
Thanks Robert Schierholz

PS sorry about the formatting meant to change that before I posted -Robert Schierholz

Well, aside from what I'm assuming is a typo ([index = size]), it looks like it should work.. - Allen Tyner

someString := '12 13 14'.
strTemp := someString.
colHex := OrderCollection new.
(strTemp findTokens: ' ') do: [ :token | colHex add: (((token asNumber) asInteger) hex) ].
Jared Parsons
Try it without while: or do: Mark Guzdial

stringToHex: aString
^ (aString findTokens: ' ') collect: [ :token | token asInteger hex ]
Alfred Park

My Solution:

stringToHex: aString
"This method returns a collection of hexadecimal equivalents of the numbers found in the string passed as a parameter."
tokenList hexNumberList
hexNumberList := OrderedCollection new.
tokenList := aString findTokens: ' '.
1 to: (tokenList size) do: [ :counter |
((tokenList at: counter) asInteger isNil) ifFalse:
[hexNumberList add: (((tokenList at: counter) asInteger) hex)]
^ hexNumberList

I've tested this little piece of code. As far as I can tell it works. If anyone can find anything wrong with this solution, let me know by sending an email to or
Christopher Henke
Okay, the previous posting of code contained pipes around tokenList and hexNumberList to declare them as temporary variables. Since the pipes are for tables, the code didn't show well. Edit the page and coping the code from there.
Christopher Henke
Christopher, did you try anyone else's solution? Like Alfred's? Mark Guzdial

Why? Are they basically the same?
Christopher Henke

I think he meant that Alfred's solution was simpler. Pavan Kavali
Why is the returned collection '16rC' '16rD'..etc instead of 'C' 'D'?

stringToHex: aString
^ (aString findTokens: ' ') collect: [ :token | token asInteger hex ]

Question about this code: What is the first line doing? I keep getting an error when I try compiling. Shouldn't it be an arguement block?

(aString findTokens: ' ')returns an ordered collection of tokens delimeted by a ' '(space) so it is an ordered collection of strings. "collect: [:token|token asInteger hex]" performs a collect operation on that ordered collection where for each of the elements in the collection, it will do an "asInteger" message to it and then to that it will to a "hex" message. I tried the code and I think "asInteger" should be "asNumber" or else you get an error. Pretty concise code.
Philip Holly

Since it's a method do we have to put it in a block like this:

stringToHex:= [ :myString|
(myString findTokens: ' ') collect: [:token |token asNumber hex]
It seems to me like Chris's solution is the kind of thing I would be able to do in a test situation. I, for one, don't know how the collect: message works off the top of my head. I will probably look it up now that i saw it here, but using only the hints for information on what understand what messages, I would have formed something like Chris's answer
a _'15 12 13 14'.
myCollection _ OrderedCollection new.
str _a findTokens:( Character space asString).
oc _ str do:[:each |
myCollection add:((((each asNumber)asInteger)asHexDigit)asString)].

a :='15 12 13 14'.
myCollection :=OrderedCollection new.
str :=a findTokens:( Character space asString).
oc := str do:[:each |
myCollection add:((((each asNumber)asInteger)asHexDigit)asString)].

car alarm

Link to this Page