Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Generate a single 32bit int from 4 8-bit ints

134 views
Skip to first unread message

Koolrans

unread,
Mar 26, 2007, 6:15:21 PM3/26/07
to
Greetings,

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,

MH

unread,
Mar 26, 2007, 8:02:43 PM3/26/07
to
In article <1174947321.7...@n76g2000hsh.googlegroups.com>,

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

Koolrans

unread,
Mar 26, 2007, 8:25:31 PM3/26/07
to
On Mar 26, 5:02 pm, mghem...@eceunix.uwaterloo.ca (MH) wrote:
> In article <1174947321.791151.249...@n76g2000hsh.googlegroups.com>,

>
>
>
> Koolrans <koolr...@gmail.com> wrote:
> >Greetings,
>
> >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)

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,


MH

unread,
Mar 26, 2007, 8:36:35 PM3/26/07
to
In article <1174955131.7...@n59g2000hsh.googlegroups.com>,

Koolrans <kool...@gmail.com> wrote:
>On Mar 26, 5:02 pm, mghem...@eceunix.uwaterloo.ca (MH) wrote:
>> In article <1174947321.791151.249...@n76g2000hsh.googlegroups.com>,

[ 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

Aric Bills

unread,
Mar 26, 2007, 8:46:20 PM3/26/07
to
On Mar 26, 4:25 pm, "Koolrans" <koolr...@gmail.com> wrote:
> On Mar 26, 5:02 pm, mghem...@eceunix.uwaterloo.ca (MH) wrote:
>
>
>
> > In article <1174947321.791151.249...@n76g2000hsh.googlegroups.com>,
>
> > Koolrans <koolr...@gmail.com> wrote:
> > >Greetings,
>
> > >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(saveto baseconvert.tcl)

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}]

Koolrans

unread,
Mar 26, 2007, 8:49:18 PM3/26/07
to
On Mar 26, 5:36 pm, mghem...@eceunix.uwaterloo.ca (MH) wrote:
> In article <1174955131.726566.118...@n59g2000hsh.googlegroups.com>,

>
> Koolrans <koolr...@gmail.com> wrote:
> >On Mar 26, 5:02 pm, mghem...@eceunix.uwaterloo.ca (MH) wrote:
> >> In article <1174947321.791151.249...@n76g2000hsh.googlegroups.com>,
>
> [ cut]
>

>


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

MH

unread,
Mar 26, 2007, 9:14:42 PM3/26/07
to
In article <1174956380.0...@b75g2000hsg.googlegroups.com>,

Aric Bills <aric....@gmail.com> wrote:
>On Mar 26, 4:25 pm, "Koolrans" <koolr...@gmail.com> wrote:
>> On Mar 26, 5:02 pm, mghem...@eceunix.uwaterloo.ca (MH) wrote:

[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

Gerald W. Lester

unread,
Mar 26, 2007, 10:00:26 PM3/26/07
to
That is a lot of work, why not do:

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

Darren New

unread,
Mar 26, 2007, 10:26:19 PM3/26/07
to
MH wrote:
> Thanks for the hint, Aric. For some reason, I never thought to check if expr
> had bitwise shifting..

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.

0 new messages