Using the struct module for socket programming i noticed that when i was using pack command with format 'BHxHH' ,after the message was packed the length was 10 bytes. But the message should be 8 bytes because 'B' is 1 byte, 'H' is 2 bytes and 'x' which is used for padding was 1 byte. After some testing i wrote the command again but changed the order in the format to 'HBxHH'and this time the message was 8 bytes. So i believe there is some issue when 'B' is in the first place of the format field.
print("Hexadecimal representation of data: ",binascii.hexlify(message))
print("length in bytes: ",len(message))`
And the output is:
Hexadecimal representation of data: b'0a00000000000a000f00'
length in bytes: 10
Hexadecimal representation of data: b'00000a000a000f00'
length in bytes: 8
Environment
CPython versions tested on: 3.9.7
Operating system and architecture: Microsoft Windows 10 Home (x64-based processor)
The text was updated successfully, but these errors were encountered:
Pankal7
changed the title
Socket programming using struct module , pack function produces wrong amount of bytes while using 'B' first in format
Socket programming using struct module , pack function produces wrong amount of bytes while using 'B' in the first place of the format field
May 24, 2022
Note By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use standard size and alignment instead of native size and alignment: see Byte Order, Size, and Alignment for details.
Bug report
Using the struct module for socket programming i noticed that when i was using pack command with format 'BHxHH' ,after the message was packed the length was 10 bytes. But the message should be 8 bytes because 'B' is 1 byte, 'H' is 2 bytes and 'x' which is used for padding was 1 byte. After some testing i wrote the command again but changed the order in the format to 'HBxHH'and this time the message was 8 bytes. So i believe there is some issue when 'B' is in the first place of the format field.
Here is a small example:
`from struct import *
import binascii
msg_op = 0
msg_id = 10
msg_number_1 =10
msg_number_2 = 15
message = pack('BHxHH',msg_id,msg_op,msg_number_1,msg_number_2)
print("Hexadecimal representation of data: ",binascii.hexlify(message))
print("length in bytes: ",len(message))
message = pack('HBxHH',msg_op,msg_id,msg_number_1,msg_number_2)
print("Hexadecimal representation of data: ",binascii.hexlify(message))
print("length in bytes: ",len(message))`
And the output is:
Hexadecimal representation of data: b'0a00000000000a000f00'
length in bytes: 10
Hexadecimal representation of data: b'00000a000a000f00'
length in bytes: 8
Environment
The text was updated successfully, but these errors were encountered: