Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageOn 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
Post Follow-up to this messageIlya 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.
Post Follow-up to this message"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
Post Follow-up to this message"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
Post Follow-up to this messageIn 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.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.