Skip to content

StrFromU64

StrFromU64

Description

Convert an unsigned 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)
                    // Create hex string for each character
                    Str hex = StrInit();
                    StrFromU64(&hex, c, &config);
                    // Ensure 2 digits with leading zero
                    if (hex.length == 1) {
                    Str          hex    = StrInit();
                    StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0};
                    StrFromU64(&hex, (u8)xs[i], &config);
                    // Ensure 2 digits with leading zero
                    if (hex.length == 1) {
        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};
        StrFromU64(&temp, *v, &config);
    
        // Merge the formatted number into output
                u64          value  = BitVecToInteger(bv);
                StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0, .use_prefix = true};
                StrFromU64(o, value, &config);
            }
        } else if (fmt_info->flags & FMT_FLAG_OCTAL) {
                u64          value  = BitVecToInteger(bv);
                StrIntFormat config = {.base = 8, .uppercase = false, .use_prefix = true};
                StrFromU64(o, value, &config);
            }
        } else {
    // ======================================
    
    Str *StrFromU64(Str *str, u64 value, const StrIntFormat *config) {
        ValidateStr(str);
    
        // Use StrFromU64 for the conversion
        StrFromU64(str, abs_value, config);
    
        // Add sign for negative decimal numbers AFTER conversion
    // Test StrFromU64 function
    bool test_str_from_u64(void) {
        WriteFmt("Testing StrFromU64\n");
    
        Str s = StrInit();
        // Test decimal conversion
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromU64(&s, 12345, &config);
        bool result = (ZstrCompare(s.data, "12345") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 0xABCD, &config);
        result = result && (ZstrCompare(s.data, "0xabcd") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 16, .uppercase = true, .use_prefix = true};
        StrFromU64(&s, 0xABCD, &config);
        result = result && (ZstrCompare(s.data, "0xABCD") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 42, &config);
        result = result && (ZstrCompare(s.data, "0b101010") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 8, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 42, &config);
        result = result && (ZstrCompare(s.data, "0o52") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromU64(&s, 0, &config);
        result = result && (ZstrCompare(s.data, "0") == 0);
        if (!result) {
            // Test decimal round-trip
            StrIntFormat config = {.base = 10, .uppercase = false};
            StrFromU64(&s, u64_values[i], &config);
            u64  recovered_u64 = 0;
            bool success       = StrToU64(&s, &recovered_u64, NULL);
            StrClear(&s);
            config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
            StrFromU64(&s, u64_values[i], &config);
            recovered_u64 = 0;
            success       = StrToU64(&s, &recovered_u64, NULL); // Can now use explicit base 16 with 0x prefix
            // Test with base value itself
            StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = (base == 2 || base == 8 || base == 16)};
            StrFromU64(&s, base, &config);
            u64 recovered = 0;
        // Test maximum u64
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromU64(&s, UINT64_MAX, &config);
        u64  recovered_max = 0;
        bool success       = StrToU64(&s, &recovered_max, NULL);
    
            StrIntFormat config = {.base = base, .uppercase = false};
            StrFromU64(&s, large_value, &config);
            u64            recovered = 0;
            StrParseConfig pconfig   = {.base = base};
            // Test StrFromU64
            StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = false};
            StrFromU64(&s, test_value, &config);
    
            // Test StrToU64 round-trip
    
            StrIntFormat config = {.base = base, .uppercase = true, .use_prefix = false};
            StrFromU64(&s, test_value, &config);
    
            u64            recovered    = 0;
    
                StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = false};
                StrFromU64(&s, test_values[i], &config);
    
                u64            recovered    = 0;
            Str          s      = StrInit();
            StrIntFormat config = {.base = 10, .uppercase = false};
            StrFromU64(&s, test_value, &config);
    
            u64  recovered = 0;
        // Test NULL string pointer - should abort
        StrIntFormat config = {.base = 10, .uppercase = false};
        StrFromU64(NULL, 42, &config);
    
        return false;
Last updated on