Hacker News new | ask | show | jobs
by magmastonealex 2544 days ago
I'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.

1 comments

An 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.
I'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);
  }