Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
up vote 1 down vote accepted

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 Sep 21 '12 at 23:59
    
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 Sep 22 '12 at 23:23
    
Works for me. Edit and post the code you're trying to use... – nneonneo Sep 23 '12 at 0:33

Your Answer

 
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.