addressv4ToBytes(): 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.
Allocate
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
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]));
optional
into: Uint8Array
The buffer to write into, whose size the caller owns; a 4-byte buffer is allocated when omitted
The four bytes written