For Programmers: Free Programming Magazines  


Home > Archive > Tcl > May 2004 > Convert hex data to numeric format









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Convert hex data to numeric format
Rob Strover

2004-05-21, 10:40 am

I have a string that contains numbers in hex format and I want to convert
them to their numeric equivalents:

eg.

set z \3 ;# 0x03
puts $z ------> output looks something like | in Windows.

if {$z == "\0"} {set x 0}
if {$z == "\1"} {set x 1}
if {$z == "\2"} {set x 2}

I know I could do a series of if statements like those above, but I'm
guessing that there is probably is far simpler, and general way, to convert
hex to decimal. I've looked at format, binary format, etc. commands but
found them hard to understand.

TIA

Rob.


Jeff Godfrey

2004-05-21, 10:40 am


"Rob Strover" <dislexic_wobmat@NOSPAMyahoo.com.invalid> wrote in message
news:oJnrc.937$L.474@news-server.bigpond.net.au...
: I have a string that contains numbers in hex format and I want to convert
: them to their numeric equivalents:
:
: eg.
:
: set z \3 ;# 0x03
: puts $z ------> output looks something like | in Windows.
:
: if {$z == "\0"} {set x 0}
: if {$z == "\1"} {set x 1}
: if {$z == "\2"} {set x 2}
:
: I know I could do a series of if statements like those above, but I'm
: guessing that there is probably is far simpler, and general way, to
convert
: hex to decimal. I've looked at format, binary format, etc. commands but
: found them hard to understand.


How about something like the following?

(bin) 2 % set string {\FF}
\FF
(bin) 3 % format "%d" [string map {"\\" "0x0"} $string]
255


Jeff


Rob Strover

2004-05-21, 11:35 am

Jeff,

I tried your idea, but it did not produce the result I wanted.

This is probably due to my not explaining things properly before. In fact,
my variable was read from a file and contains the number in byte format (as
in C or VB). The 'puts' command shows as something that looks like a thick
| symbol, not the actual hex number (within RamDebugger environment)

When I used the format & string map commands (and ran via double-click of
tcl file) I got an error message 'Expected integer but got "?'' where the ?
symbol was actually a small square (an unprintable character - which makes
sense as hex 03 is unprintable).

Still :-(

Rob.

"Jeff Godfrey" <jeff_godfrey@pobox.com> wrote in message
news:10as2t3qqce9u2d@corp.supernews.com...
>
> "Rob Strover" <dislexic_wobmat@NOSPAMyahoo.com.invalid> wrote in message
> news:oJnrc.937$L.474@news-server.bigpond.net.au...
> : I have a string that contains numbers in hex format and I want to

convert
> : them to their numeric equivalents:
> :
> : eg.
> :
> : set z \3 ;# 0x03
> : puts $z ------> output looks something like | in Windows.
> :
> : if {$z == "\0"} {set x 0}
> : if {$z == "\1"} {set x 1}
> : if {$z == "\2"} {set x 2}
> :
> : I know I could do a series of if statements like those above, but I'm
> : guessing that there is probably is far simpler, and general way, to
> convert
> : hex to decimal. I've looked at format, binary format, etc. commands but
> : found them hard to understand.
>
>
> How about something like the following?
>
> (bin) 2 % set string {\FF}
> \FF
> (bin) 3 % format "%d" [string map {"\\" "0x0"} $string]
> 255
>
>
> Jeff
>
>



Rob Strover

2004-05-21, 12:35 pm

Jeff,

Thanks for your previous help. Noting your use of 'string map' command, I
coded the following:

# Note: 'set' as done below gives the same format of data as I read from
file
(bit different than Jeff's initial 'set' cmd)
set z \3
puts $z
set x [format "%d" [string map {"\0" "0" "\1" "1" "\2" "2" "\3" "3"} $z]]
puts $x

This actually gives what I want, but is there a general string map setup
that would apply for all values of the numeric part of the map data? Or is
there another way completely?

"Jeff Godfrey" <jeff_godfrey@pobox.com> wrote in message
news:10as2t3qqce9u2d@corp.supernews.com...
>
> "Rob Strover" <dislexic_wobmat@NOSPAMyahoo.com.invalid> wrote in message
> news:oJnrc.937$L.474@news-server.bigpond.net.au...
> : I have a string that contains numbers in hex format and I want to

convert
> : them to their numeric equivalents:
> :
> : eg.
> :
> : set z \3 ;# 0x03
> : puts $z ------> output looks something like | in Windows.
> :
> : if {$z == "\0"} {set x 0}
> : if {$z == "\1"} {set x 1}
> : if {$z == "\2"} {set x 2}
> :
> : I know I could do a series of if statements like those above, but I'm
> : guessing that there is probably is far simpler, and general way, to
> convert
> : hex to decimal. I've looked at format, binary format, etc. commands but
> : found them hard to understand.
>
>
> How about something like the following?
>
> (bin) 2 % set string {\FF}
> \FF
> (bin) 3 % format "%d" [string map {"\\" "0x0"} $string]
> 255
>
>
> Jeff
>
>




Aric Bills

2004-05-21, 5:32 pm

Rob,

You mentioned that your data is binary data. With binary data, you need to
impose some kind of interpretation on it to make it meaningful. The command
that can help you do this is [binary scan].

Here's a simple example that reads a file of 8-bit binary numbers and
interprets them as unsigned integers:

proc readBinaryFile {filename} {

# open the file and get the contents
# as a binary "string"

set file [open $filename r]
fconfigure $file -translation binary
set binaryData [read $file]
close $file

# interpret the binary data
# as signed 8-bit integers

binary scan $binaryData c* intData

# convert the data to unsigned ints

set i -1
foreach int $intData {
incr i
lset intData $i [expr {$int & 0xff}]
}

return $intData
}

Regards,
Aric


Rob Strover

2004-05-22, 9:32 am

"Aric Bills" <aricb@u.wwashingtonn.edu> wrote in message
news:c8lmp6$105m$1@nntp6.u.washington.edu...
> Rob,
>
> You mentioned that your data is binary data. With binary data, you need

to
> impose some kind of interpretation on it to make it meaningful. The

command
> that can help you do this is [binary scan].
>
> Here's a simple example that reads a file of 8-bit binary numbers and
> interprets them as unsigned integers:
>
> proc readBinaryFile {filename} {
>
> # open the file and get the contents
> # as a binary "string"
>
> set file [open $filename r]
> fconfigure $file -translation binary
> set binaryData [read $file]
> close $file
>
> # interpret the binary data
> # as signed 8-bit integers
>
> binary scan $binaryData c* intData
>
> # convert the data to unsigned ints
>
> set i -1
> foreach int $intData {
> incr i
> lset intData $i [expr {$int & 0xff}]
> }
>
> return $intData
> }


Aric,

This answer has made it easier for me to see what I need to do to solve my
problem. By combining the reading of the data from the file in binary mode
*and* binary scans for the relevant input data (using c* for byte fields and
s* for integer fields) I get exactly the results I would have expected.

Thank you very much for your help [and also to Jeff for his approach using
string map]

Rob.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com