Robin K
Robin K

Reputation: 437

Why convert IP to integer for the purpose of storing it?

everyone talks about how to convert IP addresses to an integer for storing, but what are the actual reasons for this approach to be propagated? What benefits/disadvantages does it have?

In short: why should I do it?

Upvotes: 4

Views: 1954

Answers (1)

Andriy Berestovskyy
Andriy Berestovskyy

Reputation: 8544

There are few generic reasons, like integers take less space than strings, but just wanted to emphasis the main one: IP addresses are in fact numbers.

We humans prefer IP addresses string representations, like "10.0.0.1" or "2002::2" to make our life easier. In fact, most operation on IPs are strictly numeric. For example, masking (IP & mask), checking IP range (first < IP < last) etc.

So, just like string representation of a number we convert to an integer type ("123" -> 123), string representation of IP should be converted to its integer form ("10.0.0.1" -> 0x0A000001 or 167772161).

Upvotes: 9

Related Questions