I have this:
>>> a1 = pack('>L', 18)
>>> a1
b'\x00\x00\x00\x12'
And:
>>> a2 = [int(0x0), int(0x0), int(0x0), int(0x12)]
>>> a2
[0, 0, 0, 18]
Why aren't they equal?
>>> a1 == a2
False
I have this:
>>> a1 = pack('>L', 18)
>>> a1
b'\x00\x00\x00\x12'
And:
>>> a2 = [int(0x0), int(0x0), int(0x0), int(0x12)]
>>> a2
[0, 0, 0, 18]
Why aren't they equal?
>>> a1 == a2
False
Because you have to transform the second variable a2
in a byte string
if you want to be equals, a1
is a list of ints
, and a2
is a byte string
, so they will never be equeal unless you transform a2
, for example:
import struct
import array
a1 = struct.pack('>L', 18)
print(a1)
$b'\x00\x00\x00\x12'
a2 = [int(0x0), int(0x0), int(0x0), int(0x12)]
print(a2)
$[0, 0, 0, 18] #this is a list
print(a1 == a2)
$False
a3 = array.array('B',a2).tostring() #transform the list into byte
print(a3)
$b'\x00\x00\x00\x12'
print(a1 == a3)
$True
A list and a byte string aren't equivalent types, even though they're similar. To convert your list to a byte string, you can use the following methods depending on whether you're using Python 2 or 3.
Python 2:
>>> ''.join(chr(c) for c in a2)
'\x00\x00\x00\x12'
Python 3:
>>> bytes(a2)
b'\x00\x00\x00\x12'