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

TCL: Hex Binary Scan, Binary format

1,299 views
Skip to first unread message

vle...@gmail.com

unread,
Jan 3, 2007, 9:38:13 PM1/3/07
to
The following command converts a 8-character hex to a 32bit binary.

binary scan [binary format H8 $EXPECT_CRC] B32 hex2bin

The question is if EXPECT_CRC is only 7-characters (or less) for
example ABCD123, the resultant binary will be of {binary { ABCD1230 }}.
The zero is padded at the back. Any ideas on how I can get the zero
padded in front {binary { 0ABCD123 }}?

Thanks

Gerald W. Lester

unread,
Jan 4, 2007, 1:55:05 AM1/4/07
to

You need to pad EXPECT_CRC with leading zeros to 8 characters.

Thus, do:

set EXPECT_CRC [string range 00000000$EXPECT_CRC end-7 end]


binary scan [binary format H8 $EXPECT_CRC] B32 hex2bin

Note -- the command described does not convert the 8-character hex to a
32bit binary, it converts it to a 32 character string of 0/1s. To convert
to a 32bit binary (i.e. signed 32 bit integer) do:
binary scan [binary format H8 $EXPECT_CRC] I hex2bin

--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

Michael A. Cleverly

unread,
Jan 4, 2007, 2:33:49 AM1/4/07
to
On Thu, 4 Jan 2007, Gerald W. Lester wrote:

> vle...@gmail.com wrote:
> > The following command converts a 8-character hex to a 32bit binary.
> >
> > binary scan [binary format H8 $EXPECT_CRC] B32 hex2bin
> >
> > The question is if EXPECT_CRC is only 7-characters (or less) for
> > example ABCD123, the resultant binary will be of {binary { ABCD1230 }}.
> > The zero is padded at the back. Any ideas on how I can get the zero
> > padded in front {binary { 0ABCD123 }}?
>
> You need to pad EXPECT_CRC with leading zeros to 8 characters.
>
> Thus, do:
>
> set EXPECT_CRC [string range 00000000$EXPECT_CRC end-7 end]

Or:

set EXPECT_CRC [format %08s $EXPECT_CRC]

> binary scan [binary format H8 $EXPECT_CRC] B32 hex2bin
>
> Note -- the command described does not convert the 8-character hex to a
> 32bit binary, it converts it to a 32 character string of 0/1s. To convert
> to a 32bit binary (i.e. signed 32 bit integer) do:
> binary scan [binary format H8 $EXPECT_CRC] I hex2bin

Michael

vle...@gmail.com

unread,
Jan 5, 2007, 12:49:41 AM1/5/07
to
Gerald,

Thank you. I'm able to get the results i want based on your suggestion.
Btw, "end-7" flags a syntax error. I did substitution with a "string
length" instead.

Thanks again
Vincent

vle...@gmail.com

unread,
Jan 5, 2007, 1:25:03 AM1/5/07
to
Michael, thank you for your suggestion.

Donal K. Fellows

unread,
Jan 5, 2007, 8:34:20 AM1/5/07
to
vle...@gmail.com wrote:
> Btw, "end-7" flags a syntax error.

Ouch! That's a very old version of Tcl you're using there...

> I did substitution with a "string length" instead.

And that's the right workaround.

Donal.

Michael A. Cleverly

unread,
Jan 5, 2007, 1:09:33 AM1/5/07
to
On Thu, 4 Jan 2007 vle...@gmail.com wrote:

> Thank you. I'm able to get the results i want based on your suggestion.
> Btw, "end-7" flags a syntax error. I did substitution with a "string
> length" instead.

That is the case for sufficiently old versions of Tcl (pre-Tcl 8.1.1
which was released in May 1999). You are missing out on a lot of goodies
if you stick with releases from the 20th century! :-)

Michael

0 new messages