function addressv4FromBytes
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.

Examples

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

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

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);

Parameters

The buffer to read from

optional
offset: number = 0

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

Return Type

number

The address as a 32-bit unsigned integer

Throws

RangeError

If four bytes are not available at offset