Justin Carrey Justin Carrey - 1 year ago 141
Python Question

MAC address to binary bits conversion

I am trying to convert a mac address,

mac = '00:de:34:ef:2e:f4'

into binary format. And the program i am using is,

mac = '00:de:34:ef:2e:f4'
r = mac.replace(':', '').decode('hex')
print r

But i am getting a strange output when i run this program, and the output is


What am i doing wrong?

Answer Source

I don't think you want to use decode, that does unicode conversion and other stuff you don't want. Just do:

n = int(mac.replace(':', ''), 16)

Which will convert your mac address to a number. Print it with '%012x'%n to get the hex back.


If you want to convert to binary, then just do bin(n).