function addressv4ToBytes
addressv4ToBytes(
address: number,
into?: Uint8Array,
offset?: number
): Uint8Array

Writes a 4-byte IPv4 address, either into a fresh buffer or into one you supply.

The address is written in network order (big-endian). The return value is always exactly the four 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 { addressv4ToBytes } from "@hertzg/ip/bytesv4";
import { parseAddressv4 } from "@hertzg/ip/addressv4";

assertEquals(addressv4ToBytes(parseAddressv4("10.0.0.1").address), new Uint8Array([10, 0, 0, 1]));

Write into an existing frame, and get back only what was written

import { assertEquals } from "@std/assert";
import { addressv4ToBytes } from "@hertzg/ip/bytesv4";
import { parseAddressv4 } from "@hertzg/ip/addressv4";

const frame = new Uint8Array(20).fill(0xaa);
const written = addressv4ToBytes(parseAddressv4("192.168.1.1").address, frame, 6);

assertEquals(written, new Uint8Array([192, 168, 1, 1]));
assertEquals(frame.slice(4, 12), new Uint8Array([0xaa, 0xaa, 192, 168, 1, 1, 0xaa, 0xaa]));

The returned view aliases the buffer it was written into

import { assertEquals } from "@std/assert";
import { addressv4ToBytes } from "@hertzg/ip/bytesv4";

const frame = new Uint8Array(8);
const written = addressv4ToBytes(167772161, frame, 4);
written[3] = 9;

assertEquals(frame[7], 9);

Parameters

address: number

The address as a 32-bit unsigned integer

optional
into: Uint8Array

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

optional
offset: number = 0

Where in into to start writing, defaulting to 0

Return Type

The four bytes written

Throws

RangeError

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