Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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



Report this thread to moderator Post Follow-up to this message
Old Post
Ilya Ginzburg
08-21-07 09:42 AM


Re: binary scan + variable
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) an
d
>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

Report this thread to moderator Post Follow-up to this message
Old Post
Helmut Giese
08-21-07 09:42 AM


Re: binary scan + variable
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Donal K. Fellows
08-21-07 09:42 AM


Re: binary scan + variable
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Ilya Ginzburg
08-23-07 01:09 PM


Re: binary scan + variable
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Ilya Ginzburg
08-23-07 01:09 PM


Re: binary scan + variable
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 variabl
e
>a] earlier).
>But it doesn't work in this way  ;-(   - see Donal's answer.

Report this thread to moderator Post Follow-up to this message
Old Post
Cameron Laird
08-24-07 09:27 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 02:31 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.