I am using struct.pack to return me a bytes object. However, it is inconsistent.
print(struct.pack('BbbB', 0x1B, -2, 1, 0)) #returns b'\x1b\xfe\x01\x00' GOOD
print(struct.pack('BbbB', 0x0F, -2, 1, 0)) #returns b'\x0f\xfe\x01\x00' GOOD
print(struct.pack('BbbB', 0x0C, -2, 1, 0)) #returns b'\x0c\xfe\x01\x00' GOOD
print(struct.pack('BbbB', 0x35, -2, 1, 0)) #returns b'5\xfe\x01\x00' ???
print(struct.pack('BbbB', 0x21, -2, 1, 0)) #returns b'!\xfe\x01\x00' ???
If I look at ASCII character chart, I can see why it became "5" and "!" in the last two examples, but shouldnt it have returned me b'\0x35\xfe\x01\x00' and b'\0x21\xfe\x01\x00' instead of the above result? What am I missing here?
I am using Python 3.7.0.
It's not pack that is inconsistent. It's the print function that's confusing you.
Both '!' (ascii value 33, hexadecimal 0x21) and '5' (ascii value 53, hexadecimal 0x35) are printable characters, so they're just printed normally. The other bytes are not printable though, so python resorts to printing their hexadecimal representation so you can at least see something that makes sense.
The byte values are what you expect them to be, don't worry.