Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an IP address to a number:

Tags:

.net

ip

ipv6

ipv4

Question: When I convert the IP address 192.168.115.67 to a number, is it done like this:
192*2563 + 168*2562+115*2561+67*2560 = 3232265027

or like this:
192*2560 + 168*2561+115*2562+67*2563 = 1131653312

I find both variants online, and frankly it doesn't matter as long as I do all the internal IP-range comparison using the same conversion process variant. But I want to calculate the IP V6 from the IPv4 address, and it seems both variants are on the web... resulting in different IPv6 addresses, and only one can be correct...

I use the 1131653312 variant, as 1131653312 is the variant I saw .NET giving me, but 3232265027 is the variant I used when I did it in C++, and that is also the variant I find on the web for IPv4 to IPv6 conversion, and which I used before I saw that .NET uses variant 1131653312 ...

like image 254
Stefan Steiger Avatar asked May 28 '10 10:05

Stefan Steiger


1 Answers

It is definitely first one. You can ping and see how ping utility convert it to a.b.c.d notation. If you going to do this conversion I recommend expression: (a << 24) | (b << 16) | (c << 8) | d

like image 171
Andrey Avatar answered Sep 18 '22 12:09

Andrey