struct.pack() returns a blank value

328 Views Asked by At

When I try to do the following:

struct.pack("B",32)

The 32 is a int type.

I am expecting "\x20", but I get the following (blank): " "

I need this as a byte using struct() because I want to put it in a bytearray(), to look like this:

bytearray(b'\x20')

I've tried other encoding on the struct.pack() besides "B", but I could've left something out.

2

There are 2 best solutions below

1
On BEST ANSWER

If you do this:

>>> b" " == bytearray(b"\x20")
True

you will see that you are getting what you are expecting, just in a representation that you are not expecting.

0
On

space char is 0x20 on the ascii table

>>> print(b'\x20')
b' '
>>> struct.pack('B', 32)
b' '