Skip to content

StrFromI64

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 (NULL for base 10, no prefix)

Success

Returns str

Failure

Returns NULL if config is invalid

Usage example (Cross-references)

Usage examples (Cross-references)
        bool         use_prefix = (base != 10); // Add prefix for non-decimal bases
        StrIntFormat config = {.base = base, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0, .use_prefix = use_prefix};
        StrFromI64(&temp, *v, &config);
    
        // Merge the formatted number into output
    }
    
    Str *StrFromI64(Str *str, i64 value, const StrIntFormat *config) {
        ValidateStr(str);
    // Test StrFromI64 function
    bool test_str_from_i64(void) {
        WriteFmt("Testing StrFromI64\n");
    
        Str s = StrInit();
        // Test positive decimal conversion
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromI64(&s, 12345, &config);
        bool result = (ZstrCompare(s.data, "12345") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromI64(&s, -12345, &config);
        result = result && (ZstrCompare(s.data, "-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(s.data, "0") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
        StrFromI64(&s, 42, &config);
        result = result && (ZstrCompare(s.data, "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;
Last updated on