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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hex string to float

Status
Not open for further replies.

mastergriller

Programmer
Jul 1, 2005
2
US
I'm new to Tcl and need some help. I have a string that represents a float - example: bc4229d3 = -0.011850791. I can convert it to an integer but not to a float. In C I can cast it but I can't find a way to do that in Tcl. I know I can call a C routine but I would prefer to do it in Tcl only if possible. I tried everything I could find in the books I have available to me. As a final thought I tried converting it to an integer and or'ing it into a float but got an error on that.

% set x 0.0
0.0
% set y [expr $x | 0xbc42249d3]
can't use floating-point value as operand of "|"

I'm out of ideas, can anyone help?

-mastergriller
 
I found the answer to this problem.

set hex "d32942bc"
set bin [binary format H8 $hex]
binary scan $bin f* float
puts "$float"

Being so new to Tcl I don't know if there's a "gotsha" in this but it seems to work. If anyone has more information of when this won't work please let me know.

-mastergriller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top