Unpacking a hex value

Matthew Diephouse fokke_wulf at hotmail.com
Fri May 17 22:36:51 EDT 2002


The code is very much like the Perl code I submitted. It is within a 
subroutine.

def hex2bin(input)
      output = hex( input )
      output = pack("!l", output)
      ...

 From this, I get the error "required argument is not an integer", as 
previously stated. There's no other info in the Traceback that's 
important. I've tried wrapping output with a call to the int() function, 
but that doesn't work either.

You did get me on the "!l" instead of "l!", but it didn't specify order 
in the struct docs, so I was guessing.

Peter Hansen wrote:
> 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