-1

I want to write a function in python that take mac address as argument and convert that mac address to decimal form.please provide solution supported in python2

I have written a code

def mac_to_int(macaddress): 
   i=0  
   mac=list(macaddress)
   mac_int=0;   
 for i in range(len(macaddress)):
   mac_int=mac_int<<8
   mac_int+=mac[i]

return mac_int

actually in 3rd line where i want to copy content of macaddress to mac i just wanted to know that i have written correct way or not

4
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Apr 26, 2016 at 7:18
  • actually python provide support of netaddr impoting which just writing int(mac) we will get mac address in integer form but netaddr is not supported in python 2.7 thats why i am asking about some idea
    – Om Prakash
    Commented Apr 26, 2016 at 10:21
  • Well, do the maths by hand, then see if the answer matches that which your code produces. Then you will know if you have written it correctly.
    – SiHa
    Commented Apr 27, 2016 at 7:18
  • You can do this quite simply in one line, though. Investigate str.replace() and int
    – SiHa
    Commented Apr 27, 2016 at 7:23

2 Answers 2

21

It is as simple as this line:

mac_int = int(mac_str.translate(None, ":.- "), 16)

This first removes the possible byte separator characters (":", ".", "-" or " " but you can add more if you want) and then parses the string as integer with base 16 (hexadecimal).


As it has been asked for, the other way round could use e.g. str.format to convert the integer into a hexadecimal string and then just insert the colons back into it:

mac_hex = "{:012x}".format(mac_int)
mac_str = ":".join(mac_hex[i:i+2] for i in range(0, len(mac_hex), 2))
5
  • i dont think this will work in python2.7 as int(mac) fn is applicable for python 3
    – Om Prakash
    Commented Apr 27, 2016 at 11:26
  • This is no special function, it's the built-in int. It has been there forever. Commented Apr 27, 2016 at 11:28
  • @Byte Commander, is there a way to convert it back to Mac?. . I just bumped up your answer as it was very helpful to me..
    – sunny
    Commented Sep 7, 2017 at 2:18
  • 1
    @sunny I edited my answer and added some lines for that. Commented Sep 7, 2017 at 12:58
  • @Byte Commander, thank you - it worked!. Really appreciate it!.
    – sunny
    Commented Sep 7, 2017 at 23:58
0

the following function will do the trick

import re

def mac_to_int(mac):
    res = re.match('^((?:(?:[0-9a-f]{2}):){5}[0-9a-f]{2})$', mac.lower())
    if res is None:
        raise ValueError('invalid mac address')
    return int(res.group(0).replace(':', ''), 16)

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