conv

Functions for converting between integers and their string representations in decimal, hexadecimal, and binary. All output is written to caller-provided buffers.

Usage

import conv

fn main() {
    let buf: &i8 = alloc_pages(1)

    // Integer to decimal string
    let len = conv_itoa(42, buf)
    syscall(1, 1, buf, len)         // prints: 42

    // Decimal string to integer
    let n = conv_atoi("12345", 5)
    print(n)                        // 12345

    // Integer to hex
    let hlen = conv_itohex(255, buf)
    syscall(1, 1, buf, hlen)        // prints: ff

    // Hex string to integer
    let h = conv_hextoi("1a2b", 4)
    print(h)                        // 6699

    // Integer to binary
    let blen = conv_itobin(42, buf)
    syscall(1, 1, buf, blen)        // prints: 101010

    // Zero-padded decimal
    let plen = conv_itoa_pad(7, buf, 3)
    syscall(1, 1, buf, plen)        // prints: 007
}

Function Reference

FunctionSignatureDescription
conv_itoa(val: i64, dst: &i8) -> i64Integer to decimal string
conv_atoi(src: &i8, len: i64) -> i64Decimal string to integer
conv_itohex(val: i64, dst: &i8) -> i64Integer to hex string (lowercase)
conv_hextoi(src: &i8, len: i64) -> i64Hex string to integer
conv_itobin(val: i64, dst: &i8) -> i64Integer to binary string
conv_itoa_pad(val: i64, dst: &i8, width: i64) -> i64Integer to zero-padded decimal

Detailed API

conv_itoa

fn conv_itoa(val: i64, dst: &i8) -> i64

Convert an integer to its decimal string representation. Handles negative numbers (writes a leading -). The result is written to dst.

let buf: &i8 = alloc_pages(1)
let len = conv_itoa(12345, buf)
// buf[0..len] contains "12345"
print(len)   // 5

let len2 = conv_itoa(-99, buf)
// buf[0..len2] contains "-99"
print(len2)  // 3

Returns: Number of bytes written to dst.

conv_atoi

fn conv_atoi(src: &i8, len: i64) -> i64

Parse a decimal string into an integer. Handles optional leading - for negative numbers.

let n = conv_atoi("42", 2)
print(n)   // 42

let neg = conv_atoi("-100", 4)
print(neg) // -100

let zero = conv_atoi("0", 1)
print(zero) // 0

Returns: The parsed integer value.

conv_itohex

fn conv_itohex(val: i64, dst: &i8) -> i64

Convert an integer to lowercase hexadecimal string representation.

let buf: &i8 = alloc_pages(1)
let len = conv_itohex(255, buf)
// buf[0..len] contains "ff"

let len2 = conv_itohex(4096, buf)
// buf[0..len2] contains "1000"

Returns: Number of bytes written.

conv_hextoi

fn conv_hextoi(src: &i8, len: i64) -> i64

Parse a hexadecimal string into an integer. Supports both uppercase and lowercase hex digits.

let n = conv_hextoi("ff", 2)
print(n)   // 255

let n2 = conv_hextoi("1A2B", 4)
print(n2)  // 6699

Returns: The parsed integer value.

conv_itobin

fn conv_itobin(val: i64, dst: &i8) -> i64

Convert an integer to binary string representation (characters '0' and '1').

let buf: &i8 = alloc_pages(1)
let len = conv_itobin(42, buf)
// buf[0..len] contains "101010"

let len2 = conv_itobin(255, buf)
// buf[0..len2] contains "11111111"

Returns: Number of bytes written.

conv_itoa_pad

fn conv_itoa_pad(val: i64, dst: &i8, width: i64) -> i64

Convert an integer to a zero-padded decimal string of exactly width characters. If the number has fewer digits than width, leading zeros are added.

let buf: &i8 = alloc_pages(1)

let len = conv_itoa_pad(7, buf, 3)
// buf[0..len] contains "007"

let len2 = conv_itoa_pad(42, buf, 5)
// buf[0..len2] contains "00042"

let len3 = conv_itoa_pad(12345, buf, 3)
// buf[0..len3] contains "12345" (wider than pad, no truncation)

Returns: Number of bytes written (at least width).

Useful for formatting dates, times, and fixed-width numeric output.