struct.pack raises "struct.error: required argument is not an integer"
ip_header = struct.pack('!BBHHHBBH4s4s' ,version_IHL, TOS, totalLength, ID,flags, TTL,protocolNR, checksum,sourceAddress,destinationAddress)
Both the B
and H
formats require integer arguments (or non-integer objects that implement the __index__
method) (see Format Characters).
The checksum
argument is now of type bytes
because you set it here before packing:
checksum = bytes(str(checksum),"utf-8")
And bytes
objects do not implement the __index__
method.
You can check this using dir(checksum)
.
That's why you're getting the struct.error
exception.
When packing a value x using one of the integer formats ('b', 'B',
'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q'), if x is outside the valid
range for that format then struct.error is raised.
Either:
- Use a different variable for the
bytes(str(checksum),"utf-8")
output - Pass an int type object for the checksum value
Using big endian in struct pack Python with a Pointer
You may need to do this in two stages:
u1=1
u2=2
u3=3
u4=4
u5=5
point=6
data=7
# construct two separate buffers
b1 = struct.pack('>5I', u1, u2, u3, u4, u5)
b2 = struct.pack('P', point)
Then convert the pointer back to a regular uint, and then back to a pointer:
p2 = struct.unpack('I', b2)
b2a = struct.pack('>I', p2[0])
b1 = b1 + b2a
print(b1)
Output:
b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06'
Hexlify an integer in Python
You can use str.format
with x
type:
>>> '{:x}'.format(123)
'7b'
>>> '{:08x}'.format(123)
'0000007b'
or using printf
-style formatting:
>>> '%x' % 123
'7b'
>>> '%08x' % 123
'0000007b'
If you want to use binascii.hexlify
, convert the int
object to bytes
using struct.pack
:
>>> import struct
>>> import binascii
>>> struct.pack('i', 123) # Replace 123 with `file_1_size_bytes`
b'{\x00\x00\x00'
>>> binascii.hexlify(struct.pack('i', 123))
b'7b000000'
You can control byte order using >
, <
, .. format specifier:
>>> binascii.hexlify(struct.pack('>i', 123))
b'0000007b'
>>> binascii.hexlify(struct.pack('<i', 123))
b'7b000000'
See Format characters - python struct
module documentation
Python BCD splitting nibbles and concatenation 2 ints
you are interpreting the values as chars. Feeding chars to int()
won't work, you have to feed the values as strings, like so: int("0x5E", 16)
. What you are attempting is in fact int(chr(int("0x5E", 16)),16)
, which is int("^",16)
and will of course not work.
Do you expect these results?
makevalue('0x01', '0x5E') -> 350 0x15e 0b101011110
makevalue('0xFF', '0x00') -> 65280 0xff00 0b1111111100000000
makevalue('0x01', '0xFF') -> 511 0x1ff 0b111111111
makevalue('0xFF', '0xFF') -> 65535 0xffff 0b1111111111111111
If so, you can use this:
def makeValue(highbyte, lowbyte):
return int(highbyte, 16)*256 + int(lowbyte, 16)
or the IMO more ugly and errorprone:
def makeValue(highbyte, lowbyte):
return int(highbyte+lowbyte[2:], 16) # strips leading "0x" from lowbyte be4 concat
"OverflowError: Python int too large to convert to C long" on windows but not mac
You'll get that error once your numbers are greater than sys.maxsize
:
>>> p = [sys.maxsize]
>>> preds[0] = p
>>> p = [sys.maxsize+1]
>>> preds[0] = p
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long
You can confirm this by checking:
>>> import sys
>>> sys.maxsize
2147483647
To take numbers with larger precision, don't pass an int type which uses a bounded C integer behind the scenes. Use the default float:
>>> preds = np.zeros((1, 3))
Related Topics
How to Make a Smiley Face in Turtle
How to Add an Attention Mechanism in Keras
How to Shut Down a Python Simplehttpserver
How to Install a Conda Environment When Offline
How to Store the Result of an Executed Function and Re-Use Later
Counting CSV Column Occurrences on the Fly in Python
Python Dataframe Query With Spaces in Column Name
Find Records With Leading Zero in Python Pandas
Pip Installation /Usr/Local/Opt/Python/Bin/Python2.7: Bad Interpreter: No Such File or Directory
How to Append Two Bytes in Python
How to Remove Grid Lines on Image in Python
Get Usb Device Address Through Python
Convert String to Negative Number
Python - How to Sort Multidimensional List to Two-Dimensional List