struct.pack returns packed result from input value.
In [19]: pack("i",4)
Out[19]: '\x04\x00\x00\x00'
I'm trying to printout the packed result as follows:
val = pack("i", 4)
print "%d" % int(val[0])
However, I got ValueError:
ValueError: invalid literal for int() with base 10: '\x04'
How can I print the packed value?
The issue was for hexadecimal value conversion, I had to use ord() method. int() method is only for a number in a string with base 10.
In [33]: int('4')
Out[33]: 4
In [34]: ord('\x34')
Out[34]: 52
In [35]: ord('4')
Out[35]: 52
In [36]: ord('\x10')
Out[36]: 16
So, this code works.
val = pack("i", 4)
print "%d" % ord(val[0]) # -> 4
or
print "%s" % hex(ord(val[0])) # -> 0x4