addressv4FromBytes(bytes: Uint8Array,offset?: number): number
Reads a 4-byte IPv4 address from a buffer.
The four bytes at offset are read in network order (big-endian) and
combined into the 32-bit unsigned integer representation used throughout
this package.
Read an address out of a packet
Read an address out of a packet
import { assertEquals } from "@std/assert"; import { addressv4FromBytes } from "@hertzg/ip/bytesv4"; import { stringifyAddressv4 } from "@hertzg/ip/addressv4"; assertEquals(addressv4FromBytes(new Uint8Array([10, 0, 0, 1])), 167772161); assertEquals( stringifyAddressv4(addressv4FromBytes(new Uint8Array([0xaa, 0xaa, 192, 168, 1, 1]), 2)), "192.168.1.1", );
The high bit does not produce a negative number
The high bit does not produce a negative number
import { assertEquals } from "@std/assert"; import { addressv4FromBytes } from "@hertzg/ip/bytesv4"; assertEquals(addressv4FromBytes(new Uint8Array([255, 255, 255, 255])), 4294967295);
A span that runs off the end throws
A span that runs off the end throws
import { assertThrows } from "@std/assert"; import { addressv4FromBytes } from "@hertzg/ip/bytesv4"; assertThrows(() => addressv4FromBytes(new Uint8Array([1, 2, 3])), RangeError); assertThrows(() => addressv4FromBytes(new Uint8Array([1, 2, 3, 4]), 1), RangeError);
bytes: Uint8Array
The buffer to read from