Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20564
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <chris@rebertia.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'advice.': 0.05; 'brad': 0.07; 'c++,': 0.07; 'type,': 0.07; 'python': 0.08; '>>>>': 0.09; 'struct': 0.09; 'unsigned': 0.09; 'am,': 0.12; 'c++': 0.12; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; 'type:': 0.16; 'unpack': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'int': 0.18; 'cheers,': 0.20; '(most': 0.21; 'cc:no real name:2**0': 0.21; 'header:In-Reply- To:1': 0.22; 'feb': 0.22; 'int,': 0.23; 'though.': 0.23; 'traceback': 0.24; 'received:209.85.220': 0.25; 'cc:2**0': 0.26; 'code': 0.26; 'import': 0.27; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.29; 'chris': 0.30; 'signed': 0.32; 'thanks': 0.32; 'actual': 0.32; 'there': 0.33; 'fri,': 0.34; '17,': 0.34; 'integer': 0.34; 'last):': 0.34; 'equal': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'could': 0.38; 'some': 0.38; 'format': 0.38; 'think': 0.38; 'received:209': 0.39; 'easy': 0.60; 'range': 0.61; 'type': 0.61; 'more': 0.61; 'efficient': 0.62; '10:51': 0.84; '2.7.1': 0.84; 'conversion:': 0.84; 'pack,': 0.84; 'sender:addr:chris': 0.84; 'does?': 0.91; 'jul': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=SLHHT1Uk80KmX/gcija+iAShaUH6FKdos+1sOauqEig=; b=JSg1rs1RezOhhsBqefeQvUu1+CQI28BdVS4aEJvgpS3O9XI6ArFguqJMg0mMWjLr0J N+CrWl4frnPYtR3FcezTyA/lQbxBLBu7ntKzL6L1gB0XQys4iaU5W72mRjBz+pfqqv1q I1P8pHYuN7pY0uGIQgDcEYOIw9H/AdMU3iEJE= |
| MIME-Version | 1.0 |
| Sender | chris@rebertia.com |
| In-Reply-To | <436b024b-20f7-462b-a21e-11f44802e578@s7g2000vby.googlegroups.com> |
| References | <436b024b-20f7-462b-a21e-11f44802e578@s7g2000vby.googlegroups.com> |
| Date | Fri, 17 Feb 2012 11:05:23 -0800 |
| X-Google-Sender-Auth | SwRePT9WNcnvy1b1H4GVfh_ZHyw |
| Subject | Re: signed to unsigned |
| From | Chris Rebert <clp2@rebertia.com> |
| To | Brad Tilley <kj4eit@gmail.com> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Gm-Message-State | ALoCoQlEE86vd7gSTADOlXg9hcQNLQqHnXbiRkBRK4HfHzKMoQEmxqVbxaZ49xd79w0vch3E7njz |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5922.1329505526.27778.python-list@python.org> (permalink) |
| Lines | 28 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1329505526 news.xs4all.nl 6958 [2001:888:2000:d::a6]:57401 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:20564 |
Show key headers only | View raw
On Fri, Feb 17, 2012 at 10:51 AM, Brad Tilley <kj4eit@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
signed to unsigned Brad Tilley <kj4eit@gmail.com> - 2012-02-17 10:51 -0800
Re: signed to unsigned Chris Rebert <clp2@rebertia.com> - 2012-02-17 11:05 -0800
Re: signed to unsigned Brad Tilley <kj4eit@gmail.com> - 2012-02-17 11:10 -0800
Re: signed to unsigned Peter Otten <__peter__@web.de> - 2012-02-17 20:14 +0100
Re: signed to unsigned Brad Tilley <kj4eit@gmail.com> - 2012-02-17 11:22 -0800
Re: signed to unsigned Dave Angel <d@davea.name> - 2012-02-17 14:37 -0500
csiph-web