For Programmers: Free Programming Magazines  


Home > Archive > Tcl > August 2007 > binary scan + variable









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 binary scan + variable
Ilya Ginzburg

2007-08-21, 4:42 am

Hi all,

I'm trying to convert hex to binary.

the first step
binary scan \xa B* v1
works fix and returns to v1 the right value: 00001010

but when i use variable, all going wrong:
set var a
binary scan \x$var B* v1
the v1 has 0111100001100001 value


when I change the scan format to c*:
binary scan \x$var c* v1
the v1 has 120 97

in the other words, binary converts x to 120 (or to 011110000 in B mode) and
a to 97 (or to 01100001 in B mode)

The question is: how to use the substitution?

Thanks,

Ilya


Helmut Giese

2007-08-21, 4:42 am

On Tue, 21 Aug 2007 09:43:52 +0300, "Ilya Ginzburg" <ilya_g@rad.com>
wrote:

>Hi all,
>
>I'm trying to convert hex to binary.
>
>the first step
> binary scan \xa B* v1
>works fix and returns to v1 the right value: 00001010
>
>but when i use variable, all going wrong:
> set var a
> binary scan \x$var B* v1
>the v1 has 0111100001100001 value

Hi Ilya,
if you try
set var \xa
binary scan $a B* v ; puts $v
does this come closer to what you want?

>in the other words, binary converts x to 120 (or to 011110000 in B mode) and
>a to 97 (or to 01100001 in B mode)

which is quite correct since the ASCII code of 'x' is 0x78 (or decimal
120) and of 'a' it is 0x61 (decimal 97) - in other words you are
converting the characters 'x' and 'a'.

HTH
Helmut Giese
Donal K. Fellows

2007-08-21, 4:42 am

Ilya Ginzburg wrote:
> I'm trying to convert hex to binary.

[...]
> but when i use variable, all going wrong:
> set var a
> binary scan \x$var B* v1


When converting from hex to binary, you should use both [binary scan]
and [binary format], like this:

binary scan [binary format H* $var] B* v1

If you are going to be working with odd numbers of hex digits, your best
bet is to be a little bit more careful, like this:

binary scan [binary format H* $var] B[expr [string length $var]*4] v1

But you're usually working with even numbers of digits in practice, so
this level of caution is unjustified. :-)

> The question is: how to use the substitution?


Answer: don't. Use what I described above instead. (The substitution you
were using was going wrong because the Tcl parser was scanning the \x
before the $var; not what you needed, since it means that the string
starts with a literal 'x'. It is fixable with [subst], but that can have
other potential hazards too; [binary format] is better.)

Donal.
Ilya Ginzburg

2007-08-23, 8:09 am


"Donal K. Fellows" <donal.k.fellows@manchester.ac.uk> wrote in message
news:fae78c$4ln$1@godfrey.mcc.ac.uk...
> Ilya Ginzburg wrote:
> [...]
>
> When converting from hex to binary, you should use both [binary scan]
> and [binary format], like this:
>
> binary scan [binary format H* $var] B* v1
>
> If you are going to be working with odd numbers of hex digits, your best
> bet is to be a little bit more careful, like this:
>
> binary scan [binary format H* $var] B[expr [string length $var]*4] v1
>
> But you're usually working with even numbers of digits in practice, so
> this level of caution is unjustified. :-)
>
>
> Answer: don't. Use what I described above instead. (The substitution you
> were using was going wrong because the Tcl parser was scanning the \x
> before the $var; not what you needed, since it means that the string
> starts with a literal 'x'. It is fixable with [subst], but that can have
> other potential hazards too; [binary format] is better.)
>
> Donal.


Hi Donal,

Thank you for the suggestion and explanation!

Ilya


Ilya Ginzburg

2007-08-23, 8:09 am


"Helmut Giese" <hgiese@ratiosoft.com> wrote in message
news:fr3lc3l1db61o96en30aigc0otuepu7psm@
4ax.com...
> On Tue, 21 Aug 2007 09:43:52 +0300, "Ilya Ginzburg" <ilya_g@rad.com>
> wrote:
>
> Hi Ilya,
> if you try
> set var \xa
> binary scan $a B* v ; puts $v
> does this come closer to what you want?
>
> which is quite correct since the ASCII code of 'x' is 0x78 (or decimal
> 120) and of 'a' it is 0x61 (decimal 97) - in other words you are
> converting the characters 'x' and 'a'.
>
> HTH
> Helmut Giese


Hi Helmut,

Your suggestion isn't good for me, because the substitution should be
performed before \x : \x$variable -> should take me \xa (with [set variable
a] earlier).
But it doesn't work in this way ;-( - see Donal's answer.

Thank you.

Ilya


Cameron Laird

2007-08-24, 4:27 am

In article <fajqai$fou$1@news2.netvision.net.il>,
Ilya Ginzburg <ilya_g@rad.com> wrote:
>
>"Helmut Giese" <hgiese@ratiosoft.com> wrote in message
> news:fr3lc3l1db61o96en30aigc0otuepu7psm@
4ax.com...
>
>Hi Helmut,
>
>Your suggestion isn't good for me, because the substitution should be
>performed before \x : \x$variable -> should take me \xa (with [set variable
>a] earlier).
>But it doesn't work in this way ;-( - see Donal's answer.

Sponsored Links







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

Copyright 2008 codecomments.com