I have MAC address that I want to send to dpkt as raw data.
dpkt package expect me to pass the data as hex stings.
So, assuming I have the following mac address: '00:de:34:ef:2e:f4'
, written as: '00de34ef2ef4'
and I want to encode in to something like '\x00\xdeU\xef.\xf4'
and the backward translation will provide the original data.
On Python 2, I found couple of ways to do that using encode('hex')
and decode('hex').
However this solution isn't working for Python 3.
I'm haveng some trouble finding a code-snippet to support that on both versions.
I'd appriciate help on this.
Thanks
binascii
module works on both Python 2 and 3:
>>> import binascii
>>> binascii.unhexlify('00de34ef2ef4') # to raw binary
b'\x00\xde4\xef.\xf4'
>>> binascii.hexlify(_) # and back to hex
b'00de34ef2ef4'
>>> _.decode('ascii') # as str in Python 3
'00de34ef2ef4'