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!

converting integer to hex

Status
Not open for further replies.

rosskettles

Technical User
Jun 21, 2005
5
GB
Just a small problem in a program I'm doing. I need to write to a file in Hex. I have two integer values, which I want to write in the format; 0x[var1][var2] e.g 0xFF or 0xF1 et cetera.

The code I have tried so far is:

set var1 [format %x $count]
puts $out "var1"
set var2 [format %x $latest]
puts $out "var2"

unfortunately, $count is a variable of a variable! any suggestions?

(to get some context, i'm writing a program to convert .raw image files to .rle)
 
I don't understand the problem. If you do what your code suggests on an integer is the result what you want? That is:
[red]format %x 42[/red] gives [red]2a[/red]. Is that what you want?

If so, then what do you mean by a "variable of a variable"? Is $count the name of a variable whose value is an integer? Consider this:
set a 42
set count a
#now $count=a, $a=42
subst "$[set count]";# returns 42


_________________
Bob Rashkin
rrashkin@csc.com
 
Thanks for the reply, I'm finding it hard to implement the code you've given me. The problem is with the variable 'byte' (not count) it returns the variable '$b' or whatever it happens to be (it's in a foreach loop) and not the actual value of '$b' which is what I need.

I also need to put this value out to a file.

Using the subst command like this:

subst "$[set byte]"
puts $out "$subst"

results in '$$b' being put out.

Also tried:

puts $out [subst "$[set byte]"]

And various other random orders. Still not working!!
 
Tried this:
Code:
if {"[$byte==\$d]"} {
set var2 [format %x $d]
puts "$var2"

So the if statement will physicaly check if $d ($byte) is equal to the string $d. But apparently this is an invalid command name "$d==$d".

hmm.
 
Hi, I fixed the problem myself using:

Code:
if {"[expr $byte==\$d]"} {

spaces are very critical!!
 
ok, quick question. Does anyone know how to use the skip over bytes binary scan function?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top