Im struggling to understand why and how some python byte strings have \x
in them, and others dont.
For example, I have this assertion:
AssertionError:
actual b'00000001'
expected b'\x00\x00\x00\x01'
Why are they not equal? They are both byte strings, 4 bytes long with a value 1. How do I make them look the same, and how do I make them "be" the same? It seems one has come from a string string, and the other made from integer - but why dont they come out the same when converted to bytes?
Hopefully I've not missed something else.
Thanks
They are not the same thing. The \x
denotes a hexadecimal value.
Without the \x
it is just a raw byte string with each character representing its own integer value.
To demonstrate:
>>> a = b'00000001'
>>> b = b'\x00\x00\x00\x01'
>>> int.from_bytes(a, 'little')
3544385890265608240
>>> int.from_bytes(b, 'little')
16777216
>>> len(a)
8
>>> len(b)
4
>>> a.hex()
'3030303030303031'
>>> b.hex()
'00000001'
>>> a[0]
48
>>> b[0]
0
Also, since we can see from above that b'0'
is represented by 48
and since 48 = 16*3
that means b'\x30'
should be equal to b'0'
>>> b'0' == b'\x30'
True