Skip to content

Socket programming using struct module , pack function produces wrong amount of bytes while using 'B' in the first place of the format field #93184

@Pankal7

Description

@Pankal7

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

  • CPython versions tested on: 3.9.7
  • Operating system and architecture: Microsoft Windows 10 Home (x64-based processor)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions