I have a mac address in this format

00:45:36:45:f2:00  

I want to convert this mac address into byte array. I mean, by removing the ':' in between, i get total of 6 bytes and those 6 bytes should sit as six bytes in a byte array. And how is that byte array converted into an integer? Is there any in-built function in python that would do that with very very less execution time(like in microseconds)? I have never come across these concepts before. Please shed some light on this. Thanks in advance

share|improve this question

feedback

1 Answer

In Python 2.7,

macstr = addr.replace(':', '').decode('hex')

In Python 3,

import binascii
macbytes = binascii.unhexlify(addr.replace(b':', b''))
share|improve this answer
binascii works in python2 as well – jterrace yesterday
if i do in this way, total of 12 bytes are in byte array, not 6. in the mac address, f2 has to sit as 1 byte, not as 2 bytes in the byte array – Justin Carrey 13 hours ago
Works for me. Edit and post the code you're trying to use... – nneonneo 11 hours ago
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.