function addressv6FromBytes
addressv6FromBytes(
bytes: Uint8Array,
offset?: number
): bigint

Reads a 16-byte IPv6 address from a buffer.

The sixteen bytes at offset are read in network order (big-endian) and combined into the 128-bit bigint representation used throughout this package. IPv4-mapped byte sequences (::ffff:x.x.x.x) are returned as the full 128-bit value, not unwrapped to a number.

Examples

Read an address out of a packet

import { assertEquals } from "@std/assert";
import { addressv6FromBytes } from "@hertzg/ip/bytesv6";
import { stringifyAddressv6 } from "@hertzg/ip/addressv6";

// deno-fmt-ignore
const bytes = new Uint8Array([
  0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
]);

assertEquals(stringifyAddressv6(addressv6FromBytes(bytes)), "2001:db8::1");

IPv4-mapped bytes stay a 128-bit value

import { assertEquals } from "@std/assert";
import { addressv6FromBytes } from "@hertzg/ip/bytesv6";
import { stringifyAddressv6 } from "@hertzg/ip/addressv6";

// deno-fmt-ignore
const bytes = new Uint8Array([
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0xff, 0xff, 192, 168, 1, 1,
]);

assertEquals(stringifyAddressv6(addressv6FromBytes(bytes)), "::ffff:c0a8:101");

A span that runs off the end throws

import { assertThrows } from "@std/assert";
import { addressv6FromBytes } from "@hertzg/ip/bytesv6";

assertThrows(() => addressv6FromBytes(new Uint8Array(15)), RangeError);
assertThrows(() => addressv6FromBytes(new Uint8Array(16), 1), RangeError);

Parameters

The buffer to read from

optional
offset: number = 0

The offset of the first of the sixteen bytes, defaulting to 0

Return Type

bigint

The address as a 128-bit unsigned bigint

Throws

RangeError

If sixteen bytes are not available at offset