Reputation: 5146
I have couple of addresses like this -
19.22.145.103
20.52.175.104
19.92.192.102
11.20.175.108
I want to create a unique number for each of the above IP Address. I was doing it this way -
Sum of each octet 19 + 22 + 145 + 103 = 289 as the unique number.
But by this we might have same number for some IP Addresses since we are just summing the octet.
19.22.145.103 = 289
20.52.175.104 = 351
19.22.147.101 = 289
11.20.175.108 = 314
As you can see above, 289
is coming for two IP Address with my algorithm for summing the octets which is not what I want. I need a unique number which is short data type for each ip address.
Is there any better way to do this with some other formula? I thought my algorithm will guarantee uniqueness.
Upvotes: 1
Views: 2098
Reputation: 5657
If you know the set of possible IP addresses in advance and if the size of that set is less than what can be held in a short (less than 2^16) then you can look at implementing a Perfect Hash Function that is guarenteed to map each of your IP addresses onto a unique 16 bit value.
A hash function for which there are no collisions is called a "perfect hash function" (PHF). A PHF for which the hash table has no holes in it (that is, the hash table is only as big as the search list) is called a "minimal perfect hash function" (MPHF).
There are a variety of tools avialble to generate Perfect Hash Functions and a good article here.
You will have to think about the implications of this approach - if the set of IP addresses changes then the chances are that your PHF will no longer be perfect and fixing this will involve regenerating the PHF and changing the code. As stated above an IPv4 address already has a unique 32 bit representation and I'd use that if at all posible.
Upvotes: 2
Reputation: 420951
An IPv4 address is simply a 32 bit number. The aaa.bbb.ccc.ddd
is just a human readable formatting.
If you just have the octets you can transform it back to a unique 32 bit number as follows:
octet1 << 24 | octet2 << 16 | octet3 << 8 | octet4
Each octet is 8 bit (00
- FF
). You get the unique 32 bit integer by putting them side by side. The easiest way to do this is to shift them in place and or them together:
octet4 --------------------------.
octet3 -------------------. |
octet2 ----------. | |
octet1 ---. | | |
| | | |
byte 1 byte 2 byte 3 byte 4
Upvotes: 9