R: Represent address as integer

ip_to_integer {ipaddress} | R Documentation

## Represent address as integer

### Description

Encode or decode an `ip_address` as an integer.

### Usage

    ip_to_integer(x)

    integer_to_ip(x, is_ipv6 = NULL)

### Arguments

`x` | For `ip_to_integer()`: An `ip_address` vector For `integer_to_ip()`: A `bignum::biginteger` vector
`is_ipv6` | A logical vector indicating whether to construct an IPv4 or IPv6 address. If `NULL` (the default), then IPv4 is preferred but an IPv6 address is constructed when `x` is too large for the IPv4 address space.

### Details

It is common to represent an IP address as an integer, by reinterpreting the bit sequence as a big-endian unsigned integer. This means IPv4 and IPv6 addresses can be represented by 32-bit and 128-bit unsigned integers. In this way, the IPv4 addresses `0.0.0.0` and `255.255.255.255` would be represented as 0 and 4,294,967,295.

The numeric data types within base R (`integer` and `double`) have insufficient precision to cover the IPv6 address space. Instead we return a `bignum::biginteger` vector, which supports arbitrary precision integers.

### Value

- For `ip_to_integer()`: A `bignum::biginteger` vector

- For `integer_to_ip()`: An `ip_address` vector

### See Also

Other address representations: `ip_to_binary()`, `ip_to_bytes()`, `ip_to_hex()`

### Examples

    x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA))
    ip_to_integer(x)

    integer_to_ip(ip_to_integer(x))

    # with IPv4 only, we can use numeric data type
    as.numeric(ip_to_integer(ip_address("192.168.0.1")))

    integer_to_ip(3232235521)

[Package ipaddress version 0.5.2 Index]