Mac address conversion to bytes

178 Views Asked by At

I am trying to convert a mac address in hex like this: 00453645f200 to a Mac address in bytes like this: x00\x45\x36\x45\xf2\x00

But if I use binascii.unhexlify, the result is like this: b'\x00E6E\xf2\x00'. How can I get the result I want?

1

There are 1 best solutions below

0
Richard Neumann On

You can't. The python interpreter will re-format the byte string b'x00\x45\x36\x45\xf2\x00' to b'\x00E6E\xf2\x00' automatically:

>>> b'x00\x45\x36\x45\xf2\x00'
b'x00E6E\xf2\x00'

You can however, print the hexadecimal representation of each byte via:

address = 0x00453645f200
for byte in address.to_bytes(6, 'big'):
    print(hex(byte))