0

I have a mac address in the form of a string,

00:23:34:d2:a4:00 

How can this mac address index to an array?

To give a complete picture, i take the last 3 bytes in the mac address, i.e. d2:a4:00, store them in the byte array. If the corresponding bytes of d2,a4,00 are 00010001, 00110010, 00000000, then by concatenating these binary values gives me 000100010011001000000000, which if converted into an integer gives me, lets say 1000200. So, i can access the information related to that mac address using array[1000200].

If i want to do it in reverse direction, lets say i was given 1000200 number, how can i convert it into mac address 00:23:34:d2:a4:00(Assuming first 3 bytes are same for all entries). Sorry this is such a long post. It will be great help if you provide a direction. And efficiency is very important(should take very less time of execution as required for a network application). Thanks in advance.

asked Sep 23, 2012 at 16:00
5
  • 9
    Why not a dictionary? some_dict["00:23:34:d2:a4:00"] Commented Sep 23, 2012 at 16:03
  • for 1000000 entries, dictionary will be too slow. Dictionary is suited for only smaller number of items Commented Sep 23, 2012 at 16:32
  • 3
    @JustinCarrey: how so? dictionaries have O(1) lookup time. The lookup time should not increase significantly with the the size of the dict. Have you benchmarked the use of a dict versus array? Commented Sep 23, 2012 at 16:38
  • The problem is, i have to generate mac addresses. That is, i take a starting number 0, convert it into 24 bits, that is, 000000000000000000000000, and then convert it into mac address, 00:00:00:00:00:00. I cannot directly hash a mac address without doing this. Hence, i decided to go with arrays. Commented Sep 23, 2012 at 16:44
  • 3
    A dictionary will definitely not be slow as @JoelCornett says. You are talking about hashing the MAC address, I'm not sure why you feel you need to hash the address - is this required for something else in your program? If it's just for accessing, a dictionary is the better option. If you just need a hash for a mac address, why not just use the id() builtin on the mac address? Commented Sep 23, 2012 at 16:55

2 Answers 2

1

To convert the last 3 bytes in MAC to an integer and back:

mac = 'zz:zz:zz:d2:a4:00'
i = int(''.join(mac.split(':')[-3:]), 16) # integer
# -> 13804544
h = '%06x' % i
# -> 'd2a400'
mac = 'zz:zz:zz:%s:%s:%s' % (h[0:2], h[2:4], h[4:6])
# -> 'zz:zz:zz:d2:a4:00'

You should try a dictionary with MACs as keys first as @Lattyware mentioned.

To convert an integer to MAC represented as hexstring:

>>> h = '%012x' % 123
'00000000007b'
>>> ':'.join(h[i:i+2] for i in range(0, 12, 2))
'00:00:00:00:00:7b'
answered Sep 23, 2012 at 16:38
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is, i have to generate mac addresses. That is, i take a starting number 0, convert it into 24 bits, that is, 000000000000000000000000, and then convert it into mac address, 00:00:00:00:00:00. This i should do from 0 to say, 1000000. I cannot directly hash a mac address without doing this. Hence, i decided to go with arrays.
@JustinCarrey: I've added an example that shows how to convert an integer to MAC without an array.
0

Simply by using the split function, and then convert to binary by exploring ?

(Something like this:)

[int(_, 16) for _ in "00:23:34:d2:a4:00".split(':')]

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.