I would appreciate if someone can help in this.
I have a list of 4 chars
I have to generate a 32 bit integer from those 4 8-bit ints.
Example
set x {13 0 13 0}
If we concat the binary of all the 4 values of x, this will present a
32-bit value 3341. I need to generate the 32 bit integer out of x.
How can I do this?
Thanks,
Well, if 3341 is the right answer (not sure I agree), then you can do:
http://wiki.tcl.tk/16154 (save to baseconvert.tcl)
and:
source baseconvert.tcl
set numstr ""
foreach num {13 0 13 } {
set lstr "00000000"
append lstr [baseconvert 10 2 $num]
puts "lstr: $lstr"
append numstr [string range $lstr end-3 end]
puts "numstr: $numstr"
}
puts "[baseconvert 2 10 $numstr]"
This gets the answer 3341.
Now, how are you concatenating these values? 4-bits only? I thought you said
they were 8-bit ints? 3341 in binary is: 110100001101 (12 bits!), or
000011010001101 if you're concatenating them in reverse order..
1) you're treating them as 4-bit ints
2) you dropped one of the "0" values (or switched the order).
Using 8 bits,
13 = 00001101
0 = 00000000
Thus, the 32-bit value of 13 0 13 0 would be:
00001101 00000000 00001101 00000000
THIS in decimal is: 218107136
To get THAT answer, use:
foreach num {13 0 13 0} {
(i.e. add 0 back in at the end)
and change "end-3" to "end-7" in the append.
Hope that helps.
MH
Thanks for the reply. After doing some calculations, It looks like I
am wrong as indicated by you. Also did some further looking and found
something that was initially wrong.
There is a vector of uint8_t in my C++ code. For this value in C, I am
getting a list of four values in tcl as mentioned above. I have
generated the vector in C++ using a single 32bit integer and shifting
bits. I want to generate the 32 bit integer back from the tcl list.
For example, the original 32 bit value is 4525. From this, I am
getting a tcl list of {0 0 17 173}. I want to generate the original
4525 integer in tcl.
Hope I am making sense.
I will also look into the mentioned link also.
Thanks,
[ cut]
>Thanks for the reply. After doing some calculations, It looks like I
>am wrong as indicated by you. Also did some further looking and found
>something that was initially wrong.
>
>There is a vector of uint8_t in my C++ code. For this value in C, I am
>getting a list of four values in tcl as mentioned above. I have
>generated the vector in C++ using a single 32bit integer and shifting
>bits. I want to generate the 32 bit integer back from the tcl list.
But, WHERE do you want to generate it? Inside tcl, or C++? Inside tcl, your
choice is which string representation to use - binary, base 10, hex?
I'm going to assume base 10 (i.e. print as a decimal value).
>For example, the original 32 bit value is 4525. From this, I am
>getting a tcl list of {0 0 17 173}. I want to generate the original
>4525 integer in tcl.
This works, using the second example I gave you.
Keep asking, if something's not clear.
MH
You can shift bits in Tcl too...
set result 0
foreach value {0 0 17 173} {
set result [expr {($result << 8) + $value}]
}
Alternatively:
foreach {a b c d} {0 0 17 173} {}
set result [expr {($a << 24) + ($b << 16) + ($c << 8) + $d}]
>
> But, WHERE do you want to generate it? Inside tcl, or C++? Inside tcl, your
> choice is which string representation to use - binary, base 10, hex?
>
Thats right.
> I'm going to assume base 10 (i.e. print as a decimal value).
>
Thats right.
> >For example, the original 32 bit value is 4525. From this, I am
> >getting a tcl list of {0 0 17 173}. I want to generate the original
> >4525 integer in tcl.
>
> This works, using the second example I gave you.
That's right. It worked!
>
> Keep asking, if something's not clear.
>
Looks good. Thanks for you help.
[cut]
>You can shift bits in Tcl too...
>
>set result 0
>foreach value {0 0 17 173} {
> set result [expr {($result << 8) + $value}]
>}
>
>Alternatively:
>foreach {a b c d} {0 0 17 173} {}
>set result [expr {($a << 24) + ($b << 16) + ($c << 8) + $d}]
Well, then, I just gave you the long and wordy version of the result :-)
Thanks for the hint, Aric. For some reason, I never thought to check if expr
had bitwise shifting.. And yes, my bad.. >> and << have been there at least
since 7.3 (i.e. Ousterhout - my first/only Tcl book).
MH
binary scan [binary format c4 {13 0 13 0}] I X
puts $X
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Every language has bitwise shifting. It's called "multiplying by a power
of two." Note that also works regardless of endian-ness. :-)
--
Darren New / San Diego, CA, USA (PST)
"Let the wine breathe" does not mean to blow bubbles.
Trust me: your wine does not need CPR.