CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Feb 2015
Posts
1

How to generate unique number for each address?

I have couple of ip address 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 which should be short data type 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.

2. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,768

Re: How to generate unique number for each address?

Originally Posted by techy123
I have couple of ip address 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 which should be short data type for each of the above IP Address.
It is common for sizeof(short) == 2, and for the range of short to have a smaller length that the range of IPv4 addresses. Therefore, it is impossible to guarantee unique mappings from short ints to IPv4 addresses.

If you are just talking about the above IP addresses, then the solution is trivial:
Code:
```std::string addresses[] = {
"19.22.145.103",
"20.52.175.104",
"19.92.192.102",
"11.20.175.108",
};```
This maps the numbers 0 to 3 to the above IP addresses.

If you want to map integers to arbitrary IPv4 addresses, then choose an integer type that is of at least 4 8-bit bytes, e.g., map uint32_t to IPv4 addresses, since each octet corresponds to an 8-bit byte. If you do not want to use <cstdint>, then you could probably get away with unsigned int, and definitely with unsigned long.

Originally Posted by techy123
I thought my algorithm will guarantee uniqueness.
It obviously does not. Put it another way: you have a 4 "digit" number in base 256. Consider a 4 digit number in base 10, e.g., 1234. It should be obvious that merely summing the digits of 1234 does not guarantee uniqueness since the result will be the same for 4321. Likewise, this holds for 4 "digit" numbers in base 256.

Originally Posted by techy123
Is there any better way to do this with some other formula?
Yes, i.e., make use of the place-value system. In A.B.C.D, you could let D be the one's place, C be the 256's place, B be the 256**2's place, and A be the 256**3's place (where ** denotes exponentiation), thereby converting this 4 "digit" number in base 256 to base 10 through multiplication and addition.
Last edited by laserlight; February 10th, 2015 at 04:42 AM.

3. Re: How to generate unique number for each address?

I want to create a unique number which should be short data type for each of the above IP Address.
How is this number going to be used for what purpose?

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•