function stringNT
stringNT(): Coder<string>

Creates a Coder for null-terminated strings.

The string is encoded as UTF-8 bytes followed by a null byte (0x00). Decoding reads until a null byte is encountered.

Examples

Example 1

import { assertEquals } from "@std/assert";
import { stringNT } from "@hertzg/binstruct/string";
import { struct } from "@hertzg/binstruct/struct";
import { u16le, u32le } from "@hertzg/binstruct/numeric";

// Define a network packet structure with null-terminated strings
const networkPacketCoder = struct({
  packetId: u32le(),           // Packet identifier
  sourceAddress: stringNT(),   // Source IP address (null-terminated)
  destinationAddress: stringNT(), // Destination IP address (null-terminated)
  protocol: u16le(),           // Protocol type
  payload: stringNT(),         // Payload data (null-terminated)
});

// Create sample network packet
const packet = {
  packetId: 12345,
  sourceAddress: "192.168.1.100",
  destinationAddress: "192.168.1.200",
  protocol: 80, // HTTP
  payload: "GET /index.html HTTP/1.1",
};

const buffer = new Uint8Array(500);
const bytesWritten = networkPacketCoder.encode(packet, buffer);
const [decoded, bytesRead] = networkPacketCoder.decode(buffer);
assertEquals(decoded, packet);
assertEquals(bytesWritten, bytesRead);

Return Type

Coder<string>

A Coder that can encode/decode null-terminated strings

Usage

import { stringNT } from "mod.ts";