Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making strings into numbers

Status
Not open for further replies.

tomdagliusa

Programmer
May 31, 2000
34
US
If I parse the expect_out buffer and extract a string, how do I then convert that string into a number?

My goal is to extract the string: ex. 01234567
Then convert the string into hex: ex. 0x01234567
Then do ands or or's with the hex value:
set hi_word_mask "FFFF0000"
set hi_word_value [expr $hi_word_mask & $hex_value]

Now that I type the question, I can think of another.
How do I make my variable "hi_word_mask" a number,
and not a string?

Thanks,
Tom
 
Tcl knows only strings, lists and arrays, defaulting to strings.

When you write: set x 123, this means: set x "123".

But expr knows about numbers; decimal, octal AND hexadecimal numbers.
For hexadecimal numbers simply prepend 0x to the numeral string before using expr (more in the expr page).

set x1 0x1234
set x2 0x1111
set or [expr {$x1 | $x2}]
puts [format "0x%x" $or]

And you should get : 0x1335

ulis
 
Ulis,

Thanks. I played around with variations of what you
wrote before typing my question, but your example
helped greatly.

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top