-
February 10th, 2015, 01:38 AM
#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.
-
February 10th, 2015, 04:37 AM
#2
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.
-
February 10th, 2015, 05:02 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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|