Unpacking a hex value

Peter Hansen peter at engcorp.com
Fri May 17 22:11:34 EDT 2002


Matthew Diephouse wrote:
> 
> I have the following perl code, which I'm trying to translate to python:
> 
> my $out = hex( shift(@_) );
> $out = pack("N", $out);
> 
> However, I can't figure out how to pack the hex value. I always get an
> error: "required argument is not an integer". Python's pack doesn't have
> an "N" format, but I think I could use "l!" Help? Please?

Please post your code and the exception traceback so we can tell what 
the problem is directly, rather than guessing.

By the way, does this help?:

>>> import struct
>>> struct.pack('l', '5')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
struct.error: required argument is not an integer
>>> struct.pack('l', 5)
'\x05\x00\x00\x00'

And you know about the documentation for struct, right?  It tells
what "l" means, and the others.  And did you really mean "!l"?

http://www.python.org/doc/current/lib/module-struct.html

-Peter



More information about the Python-list mailing list