-2

I'm having a really weird issue with a built-in Python3.8 object that I'm not sure how to get around.

I was able to isolate the issue in my script to the append method of bytearray(). If I try to append any value that is between 32 and 126 to my bytearray, instead of appending hexadecimal it appends some other seemingly arbitrary character. Instead of appending 32 as \x20 and 126 as \x7e, it appends and ~ respectively.

It still exhibits this behavior if I specify the hex I want to append as actual hex instead of using ints as well. I just happened to have spent the last three to four hours using this function and somehow avoided test cases that included any byte value between 32-126.

3
  • "instead of appending hexadecimal" you don't append "hexadecimals" to byte arrays. You append bytes, with the value of the corresponding int objects. These aren't arbitrary characters, they are characters that correspond to the unicode code point of the characters you see (basically, printable ascii characters will be represented as those characters when you print bytearray/bytes objects). This is just how these objects are represented. Commented Oct 27, 2020 at 5:09
  • I did a little research off what you said and found this. stackoverflow.com/questions/17093700/python-bytearray-printing Thank you. Commented Oct 27, 2020 at 5:19
  • Does this answer your question? Python Bytearray Printing
    – Scratte
    Commented Oct 27, 2020 at 8:39

1 Answer 1

0

What you are seeing is a just the way Python displays byte strings by default. Byte strings are displayed as ASCII when the byte represents a printable ASCII character (32-126). Unprintable bytes display as \xnn where nn is the hexadecimal value of the byte.

There are other ways to access the bytes that have different default display methods:

>>> b=bytearray()
>>> b.append(32)  # 32 is the ASCII code for a space.
>>> b
bytearray(b' ')
>>> b.append(48)  # 48 is the ASCII code for a zero.
>>> b
bytearray(b' 0')
>>> b.append(1)   # 1 is an unprintable ASCII code.
>>> b
bytearray(b' 0\x01')
>>> b.hex()       # display the 3-byte array as only hexadecimal codes
'203001'
>>> b[0]          # display the individual bytes in decimal
32
>>> b[1]
48
>>> b[2]
1
>>> list(b)
[32, 48, 1]

If you want a different display than the default, write a function and customize it. For example, this prints similar to the existing default for bytearray, but prints all bytes as \xnn escape codes:

>>> def display(b):
...   return "bytearray(b'" + ''.join([f'\\x{n:02x}' for n in b]) + "')"
...
>>> print(display(b))
bytearray(b'\x20\x30\x01')
2
  • I think it is misleading to state that b[0] "displays" the individual byte in decimal. Indexing into a bytearray or bytes objects returns an int object. int objects are displayed in decimal format. Commented Oct 27, 2020 at 5:22
  • @juanpa.arrivillaga I'm commenting on what the REPL is doing. b[0] returns an int object, but the REPL calls int.__repr__() on the object and returns a string in decimal. In actual code, you'd call print(b[0]) and print would call int.__str__() and do the same thing. Commented Oct 27, 2020 at 5:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.