function addressv6ToBytes
addressv6ToBytes(
address: bigint,
into?: Uint8Array,
offset?: number
): Uint8Array

Writes a 16-byte IPv6 address, either into a fresh buffer or into one you supply.

The address is written in network order (big-endian) as four 32-bit chunks. The return value is always exactly the sixteen bytes written: a fresh Uint8Array when into is omitted, and a view into into when it is given — never the whole of into. Writing through that view writes into into.

Examples

Allocate

import { assertEquals } from "@std/assert";
import { addressv6ToBytes } from "@hertzg/ip/bytesv6";
import { parseAddressv6 } from "@hertzg/ip/addressv6";

// deno-fmt-ignore
assertEquals(addressv6ToBytes(parseAddressv6("2001:db8::1").address), new Uint8Array([
  0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
]));

Write into an existing frame

import { assertEquals } from "@std/assert";
import { addressv6ToBytes } from "@hertzg/ip/bytesv6";
import { parseAddressv6 } from "@hertzg/ip/addressv6";

const frame = new Uint8Array(40);
const written = addressv6ToBytes(parseAddressv6("::1").address, frame, 8);

assertEquals(written.length, 16);
assertEquals(frame[23], 1);

Parameters

address: bigint

The address as a 128-bit unsigned bigint

optional
into: Uint8Array

The buffer to write into, whose size the caller owns; a 16-byte buffer is allocated when omitted

optional
offset: number = 0

Where in into to start writing, defaulting to 0

Return Type

The sixteen bytes written

Throws

RangeError

If the address is out of range, or sixteen bytes are not available at offset

Usage

import { addressv6ToBytes } from "mod.ts";