0

Hopefully this is a quick question/answer, but it is very tough to google.

If i have this:

b'\xff\xfe\x03\x00\x07\xff0\x00[\x0f\xefm'

What does the "extra" characters at the back of the hex mean for the following four values:

\xff0 \x00[ \x0f \xefm

Any help would be appreciated! Note: This is Python 3.5.

Aerophilic
  • 885
  • 2
  • 9
  • 22
  • 1
    they aren't extra characters. Python `bytes` objects will print all ascii-printable code points as their ascii characters, so try `print(b'\x41')` and it will print `A` – juanpa.arrivillaga Oct 01 '18 at 04:20
  • So how do I interpret them in terms of raw "values? For example, I know xff is 255, but what is \xff0? – Aerophilic Oct 01 '18 at 04:20
  • It's 48 (in decimal) – juanpa.arrivillaga Oct 01 '18 at 04:21
  • use `list(my_bytes_object)` to create a list of integers. – juanpa.arrivillaga Oct 01 '18 at 04:22
  • Will try that, but how many bytes do those actually correspond to? I am expecting 16-bit integers... but that should be fully encompassed in a 0-f, 0-f. Is this something "odd" that is going on with Python? – Aerophilic Oct 01 '18 at 04:23
  • No. What do you mean 16 bit integers? Were those bytes suppose to be a buffer holding 16 bit integers? If you want that, you probably want to look at the `struct` module convert into python `int` objects – juanpa.arrivillaga Oct 01 '18 at 04:24
  • I am modifying an existing code base, and this is what is getting pumped to me over the line. This byte string represents a packet, where the first three hex values are the header, the next 5 are payload, and the last is the checksum. – Aerophilic Oct 01 '18 at 04:26
  • Yeah, look into the `struct` module for this. But you have 12 bytes there. you can use `len` on the bytes-object – juanpa.arrivillaga Oct 01 '18 at 04:27
  • Got it re: using struct, but to make sure I have a concrete answer to my original question: the "extra" character at the end of the hex is simply python trying to make it "easier" for me to understand what it is by automatically interpreting the hex for me? If so, please answer with that, and I'll make it the accepted answer... – Aerophilic Oct 01 '18 at 04:31
  • Yes. This is definitely a duplicate though, although as you found, it is hard to google. Been looking for a good target – juanpa.arrivillaga Oct 01 '18 at 04:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181048/discussion-between-aerophilic-and-juanpa-arrivillaga). – Aerophilic Oct 01 '18 at 04:36
  • It is not "interpreting hex for you", as the underlying implementation is not hex. It will show ASCII characters when they are printable (between 32 and 127), and hex escapes when they are not (between 0 and 31, and between 128 and 255). So `\xff` is 255, and `0` is 48. – Amadan Oct 01 '18 at 04:36

1 Answers1

2
>>> chars = b'\xff\xfe\x03\x00\x07\xff0\x00[\x0f\xefm'
>>> [chr(b) for b in chars]
['ÿ', 'þ', '\x03', '\x00', '\x07', 'ÿ', '0', '\x00', '[', '\x0f', 'ï', 'm']

Compare the string with the resulting list, one character at a time. You can see, for example, that \xff becomes the "char" ÿ and that [ is represented the same for both.

If you want a more readable representation, use hex encoding:

>>> chars.hex()
'fffe030007ff30005b0fef6d'

which shows that the [ could also be represented as \x5b (it is just before the \x0f byte).

jnnnnn
  • 3,889
  • 32
  • 37