Skip to main content
CString wraps a raw C string pointer (const char*) and provides safe read access.

Example

import { dlopen, FFIType, CString } from 'arkffi';

const lib = dlopen('libffi_target.so', {
  getVersion: { args: [], returns: FFIType.int64 },
});

const ptr: number = lib.symbols.getVersion();
const cstr = new CString(ptr);

cstr.toString(); // → "1.0.0"
cstr.length;     // → 5

lib.close();

Constructor

constructor(ptr: number, byteOffset?: number, byteLength?: number);
ParameterTypeDefaultDescription
ptrnumberMemory address of the C string
byteOffsetnumber0Skip N bytes before reading
byteLengthnumberauto-detect (null-terminated)Explicit read length
const cstr = new CString(ptr, 0, 5);
cstr.toString(); // reads first 5 bytes

Properties

length

get length(): number;
Returns the length of the C string in characters.

Methods

toString()

toString(): string;
Reads the C string and returns it as a TypeScript string.

Null Pointers

Passing 0 is safe and returns an empty string:
const cstr = new CString(0);
cstr.toString(); // → ""
cstr.length;     // → 0