Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert hex to ascii?

3,403 views
Skip to first unread message

penci...@yahoo.com

unread,
Aug 12, 2012, 7:08:48 PM8/12/12
to
tcl newbie and i'm baffled. i have a string of hex characters and i want to convert them to the ascii.

input: 61 62 63
output: abc

i must have missed something on page 1 of the manual because no searches seem to be able to tell me how to do this. the closest i have come is that 'puts' does some auto conversion, but i want to use the new value, not print it.

set a "0x61 0x62 0x63"
puts "$a = [binary format c* $a]"

it seems 'puts' does autoconversion to unicode. i'm looking for the tcl command to do this.

i also found a way to convert a single character, but there must be a more efficient way than this:

set a "65"
set a [format %c $a]

help?

tim
;)

Gerry Snyder

unread,
Aug 12, 2012, 9:14:45 PM8/12/12
to
On 8/12/2012 4:08 PM, penci...@yahoo.com wrote:
> tcl newbie and i'm baffled. i have a string of hex characters and i want to convert them to the ascii.
>
> input: 61 62 63
> output: abc
>
> i must have missed something on page 1 of the manual because no searches seem to be able to tell me how to do this. the closest i have come is that 'puts' does some auto conversion, but i want to use the new value, not print it.
>
> set a "0x61 0x62 0x63"
> puts "$a = [binary format c* $a]"

You were looking right at your answer.

> 1 % set a "0x61 0x62 0x63"
0x61 0x62 0x63
> 2 % set b [binary format c* $a]
abc

[binary format] did all the work.

HTH,

Gerry

Zbigniew Diaczyszyn

unread,
Aug 13, 2012, 4:14:26 AM8/13/12
to
Am 13.08.2012 03:14, schrieb Gerry Snyder:
> but i want to use the new value, not print it.

Did you try:

% scan 0x61 %x
97

Uwe Klein

unread,
Aug 13, 2012, 5:20:33 AM8/13/12
to
penci...@yahoo.com wrote:
> tcl newbie and i'm baffled. i have a string of hex characters and i want to convert them to the ascii.
>
> input: 61 62 63
> output: abc

set hex {{} 61 62 63 64 65 66 67 68 69}

set ascii [ subst [ join $hex \\x ] ]

uwe

penci...@yahoo.com

unread,
Aug 13, 2012, 6:41:21 AM8/13/12
to
it works - thank you!

i thought it tried that... *blush*

tim
;)

Andreas Leitgeb

unread,
Aug 13, 2012, 4:34:36 PM8/13/12
to
penci...@yahoo.com <penci...@yahoo.com> wrote:
> tcl newbie and i'm baffled. i have a string of hex characters and i want to convert them to the ascii.
> input: 61 62 63
> output: abc

To add yet another one...

set x "61 62 63"
set y [join $x ""];# that makes it "616263"
binary format H* $y ;# returns "abc"

0 new messages