Skip to content

StrFromI64

Description

Convert a signed 64-bit integer to string

Parameters

Name Direction Description
str out String to store the result in
value in Value to convert
config in Formatting configuration. May be NULL (uses the default: base 10, no prefix).

Success

Returns str

Failure

Returns NULL on allocator OOM while appending to str, or when a non-NULL config has an invalid base. The destination string is untouched.

Usage example (Cross-references)

Usage examples (Cross-references)
        // allocator needed -- `o` may itself be a stack Str.
        StrInitStack(temp, 80) {
            if (!StrFromI64(&temp, *v, &config)) {
                return false;
            }
    }
    
    Str *StrFromI64(Str *str, i64 value, const StrIntFormat *config) {
        ValidateStr(str);
    // Test StrFromI64 function
    bool test_str_from_i64(void) {
        WriteFmt("Testing StrFromI64\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        // Test positive decimal conversion
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromI64(&s, 12345, &config);
        bool result = (ZstrCompare(StrBegin(&s), "12345") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromI64(&s, -12345, &config);
        result = result && (ZstrCompare(StrBegin(&s), "-12345") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
        StrFromI64(&s, -0xABCD, &config);
        // For negative numbers in non-decimal bases, it uses unsigned representation
        // -0xABCD = -(43981) = large positive number when treated as unsigned
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromI64(&s, 0, &config);
        result = result && (ZstrCompare(StrBegin(&s), "0") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
        StrFromI64(&s, 42, &config);
        result = result && (ZstrCompare(StrBegin(&s), "0b101010") == 0);
        if (!result) {
            // Test decimal round-trip
            StrIntFormat config = {.base = 10, .uppercase = false};
            StrFromI64(&s, i64_values[i], &config);
            i64  recovered_i64 = 0;
            bool success       = StrToI64(&s, &recovered_i64, NULL);
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromI64(&s, INT64_MIN + 1, &config);
        i64 recovered_min = 0;
        success           = StrToI64(&s, &recovered_min, NULL);
        // Test StrFromI64 with NULL pointer - should abort
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromI64(NULL, 42, &config);
    
        return false;
    // be the full magnitude.
    static bool test_from_i64_int64_min_exact_digits(void) {
        WriteFmt("Testing StrFromI64(INT64_MIN) exact digits\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str          s      = StrInit(&alloc);
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromI64(&s, INT64_MIN, &config);
        bool result = (ZstrCompare(StrBegin(&s), "-9223372036854775808") == 0);
Last updated on