« Tcl - lappend v concat | Main | Good follow up ... my thanks »

June 28, 2006

Converting between Hex and Decimal in Tcl

Tcl is much used in the semiconductor industry, so there's a common requirement to use it to convert values back and forth between hexadecimal (base 16) and decimal (base 10).

Easily done! The format command can be used to convert from decimal to hexadecimal, and the expr command can be used to convert in the opposite direction ... Let's see an example:

set initial 157

# Decimal to hex using format
set inhex [format %x $initial]

# Hex to decimal using expr
set back_in_decimal [expr 0x$inhex]

# checking the results
puts "$initial ... $inhex ... $back_in_decimal"

When I run that, I get ...

157 ... 9d ... 157

Posted by gje at June 28, 2006 12:12 AM

Comments

Post a comment




Remember Me?


Well House Consultants Ltd. Copyright 2008