0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
NMSilva
  • 3
  • 2
  • If you print the content of a bytearray, all bytes whose values correspond to an ASCII character are represented by that character, for convenience. – Thierry Lathuille Feb 22 '19 at 10:00

2 Answers2

0

space char is 0x20 on the ascii table

>>> print(b'\x20')
b' '
>>> struct.pack('B', 32)
b' '
ShmulikA
  • 3,468
  • 3
  • 25
  • 40
0

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.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Yes, I what you mean now. I was concerned that this was being converted to some other type of value which wasn't what I wanted. Other people's answers/comments also say the same, but I'll mark your answer as it is the clearest in explaining this so far. Many thanks! – NMSilva Feb 22 '19 at 10:52