Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > signed to unsigned

Reply
Thread Tools

signed to unsigned

 
 
Brad Tilley
Guest
Posts: n/a
 
      02-17-2012
In C or C++, I can do this for integer conversion:

unsigned int j = -327681234; // Notice this is signed.

j will equal 3967286062. I thought with Python that I could use struct
to pack the signed int as an unsigned int, but that fails:

>>> x = struct.pack("<I", -327681234)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: integer out of range for 'I' format code

Is there an easy way in Python to do the same conversion that C or C++
code does? Thanks for any advice.
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-17-2012
On Fri, Feb 17, 2012 at 10:51 AM, Brad Tilley <(E-Mail Removed)> wrote:
> In C or C++, I can do this for integer conversion:
>
> unsigned int j = -327681234; // Notice this is signed.
>
> j will equal 3967286062. I thought with Python that I could use struct
> to pack the signed int as an unsigned int, but that fails:
>
>>>> x = struct.pack("<I", -327681234)

> Traceback (most recent call last):
> *File "<stdin>", line 1, in <module>
> struct.error: integer out of range for 'I' format code
>
> Is there an easy way in Python to do the same conversion that C or C++
> code does? Thanks for any advice.


Pack it as the actual type, then unpack it as the desired type:

Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
Type "help", "copyright", "credits" or "license" for more information.
>>> from struct import pack, unpack
>>> unpack('=I', pack('=i',-327681234))

(3967286062,)

I would think there's some more efficient way to do this though.

Cheers,
Chris
 
Reply With Quote
 
 
 
 
Brad Tilley
Guest
Posts: n/a
 
      02-17-2012
> Pack it as the actual type, then unpack it as the desired type:
>
> Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
> Type "help", "copyright", "credits" or "license" for more information.>>> from struct import pack, unpack
> >>> unpack('=I', pack('=i',-327681234))

>
> (3967286062,)
>
> I would think there's some more efficient way to do this though.
>
> Cheers,
> Chris



Thanks Chris! I was doing it backwards. I only have a few of these
right now, so performance isn't a concern. I appreciate the advice.

Brad
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      02-17-2012
Brad Tilley wrote:

> In C or C++, I can do this for integer conversion:
>
> unsigned int j = -327681234; // Notice this is signed.
>
> j will equal 3967286062. I thought with Python that I could use struct
> to pack the signed int as an unsigned int, but that fails:
>
>>>> x = struct.pack("<I", -327681234)

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> struct.error: integer out of range for 'I' format code
>
> Is there an easy way in Python to do the same conversion that C or C++
> code does? Thanks for any advice.


>>> 0xffffffff & -327681234

3967286062


 
Reply With Quote
 
Brad Tilley
Guest
Posts: n/a
 
      02-17-2012

> >>> 0xffffffff & -327681234

>
> 3967286062


Very nice! Thanks for that example. Unsigned long longs:

0xffffffffffffffff & -9151314442815602945
9295429630893948671L
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      02-17-2012
On 02/17/2012 02:22 PM, Brad Tilley wrote:
>>>>> 0xffffffff& -327681234

>> 3967286062

> Very nice! Thanks for that example. Unsigned long longs:
>
> 0xffffffffffffffff& -9151314442815602945
> 9295429630893948671L

Or more generally, use modulo

-13452324 % 2^64

--

DaveA

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Convert a signed binary number into a signed one ? Rob1bureau VHDL 1 02-27-2010 12:13 AM
signed(12 downto 0) to signed (8 downto 0) kyrpa83 VHDL 1 10-17-2007 06:58 PM
signed to unsigned Patrick VHDL 1 06-07-2004 01:59 PM
STD_LOGIC_VECTOR vs. UNSIGNED vs. SIGNED Jeremy Pyle VHDL 3 06-28-2003 10:47 PM



Advertisments