4 ms·
I believe you can also represent an ipv4 address in base 10, by doing something like a.b.c.d, (a * 2^24) + (b * 2^16) + (c * 2^8) + (d * 2^0) (the first time
by digitalsushi 7y ago
I believe you can also represent an ipv4 address in base 10, by doing something like
a.b.c.d, (a * 2^24) + (b * 2^16) + (c * 2^8) + (d * 2^0)
(the first time i did this was the first time I understood raising a constant to zero was one, so I left it there for myself)
The resultant number should be somewhere between 0 and (2^32)-1. It's a neat toy. I'm not sure what value it has in practice.
192 x 2^24 + 168 x 2^16 + 0 x 2^8 + 1 x 2^0 == 3232235521
ping 3232235521
PING 3232235521 (192.168.0.1): 56 data bytes
Request timeout for icmp_seq 0
^C
- magmastonealex 7y agoI've used this a handful of times, actually! I've mostly done it to determine a "next" IP address from a large range - i.e, what IP comes after 10.0.0.255, and how can I bound it between 10.0.0.30 and 10.0.1.128? It's much easier to increment an integer, compare it to other integers, and then convert it back to an string-format IP than it is to implement those operations by "parsing" IP addresses.
- tomjakubowski 7y agoAn IP address like 127.0.0.1 is an integer, written in an odd base 256 notation! What I think you mean is that it's easier to increment an integer written in conventional base 10, because people are used to working with numbers like that.
- jolmg 7y agoI'm imagining magmastonealex found it useful for things like working with each of 10.0.0.0/16 without having to manage the digits of those base-256 numbers: for (var i = 0; i < 2 ** 16; i++) { work("10.0." + i); } as opposed to for (var i = 0; i < 2 ** 16; i++) { work("10.0." + i / 256 + "." + i % 256); }
- maddyboo 7y agoThis is neat! Here's a JS version: > const ip2dec = ip => ip.split(".").reverse().reduce((acc, n, i) => acc + (parseInt(n) * (2 ** (i * 8))), 0) > ip2dec("192.168.0.1") 3232235521 > ip2dec("192.168.1.1") 3232235777 > ip2dec("0xDE.0xAD.0xBE.0xEF") 3735928559