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
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|
+------------------------------------------------------------------------+
> 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
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
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.
> 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