ip_to_integer: Represent address as integer

Description Usage Arguments Details Value See Also Examples

View source: R/ip_to_integer.R

Description

Encode or decode an ip_address as an integer.

Usage

1
2
3

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

See Also

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

Examples

1
2
3
4
5
6
7
8
9
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)

ipaddress documentation built on July 27, 2021, 9:06 a.m.