简体   繁体   中英

Mac address conversion to bytes

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?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM