D
D
DontQuantum2019-11-10 11:47:50
Computer networks
DontQuantum, 2019-11-10 11:47:50

How to count the number of combinations in an ip range?

There is such a range - 217.28.250.232 - 217.28.252.215
1) 217.28.250.232 - 217.28.252.255 = 23
2) 217.28.251.0 - 217.28.252.255 = 255 3) 217.28.252.0 - 217.252.215
= 215
in the amount of 493 in the amount of
On paper, it doesn't seem difficult. But what formula would you use for this? 0_o

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Somewhere Intech, 2019-11-10
@DontQuantum

In JS: (language was not specified here)

const ip2int = (ip) => 
  ip.split('.')
  .map( (value, index) => Number(value) * 256 ** (3-index))
  .reduce( (sum,value) => sum + value, 0);
const range = (from, to) => ip2int(to) - ip2int(from);
console.log(range("192.168.1.0", "192.168.1.255"));
// 255 - т.е. броадкаст адрес он тоже считает

The bottom line is to convert ip to integers and subtract them
The universal and simplest algorithm for converting ip to a number, for example 5.24.0.0 :
5 * (256^3) + 24 * (256^2) + 0 * 256 + 0 = 85458944

D
dollar, 2019-11-10
@dollar

Formula from your paper. Or is the formula in one line important to you?
Although, you also want to calculate the sum of the ranges, perhaps arbitrary (without a mask), where already there in one line.
In general, if there is an algorithm, then you can make a calculator. And whether there will be 10 lines of code or 200 is not so important.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question