StrToU64

Table of Contents

StrToU64

Description

Convert string to unsigned 64-bit integer

Parameters

NameDirectionDescription
strinString to convert
valueoutWhere to store the result
configinParse configuration (NULL for auto-detect base)

Success

Returns true and stores result in value

Failure

Returns false if conversion fails

Usage example (Cross-references)

    // Use base 0 to let strtoul detect the base from prefix
    u64 val;
    if (!StrToU64(&temp, &val, NULL)) {
    LOG_ERROR("Failed to parse u8");
    StrDeinit(&temp);
    // Use base 0 to let strtoul detect the base from prefix
    u64 val;
    if (!StrToU64(&temp, &val, NULL)) {
    LOG_ERROR("Failed to parse u16");
    StrDeinit(&temp);
    // Use base 0 to let strtoul detect the base from prefix
    u64 val;
    if (!StrToU64(&temp, &val, NULL)) {
    LOG_ERROR("Failed to parse u32");
    StrDeinit(&temp);
    
    // Use base 0 to let strtoul detect the base from prefix
    if (!StrToU64(&temp, v, NULL)) {
    LOG_ERROR("Failed to parse u64");
    StrDeinit(&temp);
    u64            value;
    StrParseConfig config = {.base = 16};
    if (!StrToU64(&hex_str, &value, &config)) {
    LOG_ERROR("Failed to parse hex value");
    StrDeinit(&hex_str);
    u64            value;
    StrParseConfig config = {.base = 8};
    if (!StrToU64(&oct_str, &value, &config)) {
    LOG_ERROR("Failed to parse octal value");
    StrDeinit(&oct_str);
    }
    
    bool StrToU64(const Str* str, u64* value, const StrParseConfig* config) {
    ValidateStr(str);
    
    u64 unsigned_value;
    if (!StrToU64(&temp_str, &unsigned_value, config)) {
    return false;
    }
    // Test StrToU64 function
    bool test_str_to_u64(void) {
    printf("Testing StrToU64\n");
    
    // Test decimal conversion
    Str  s       = StrInitFromZstr("12345");
    u64  value   = 0;
    bool success = StrToU64(&s, &value, NULL);
    bool result  = (success && value == 12345);
    s                     = StrInitFromZstr("ABCD"); // No 0x prefix when base is explicitly 16
    StrParseConfig config = {.base = 16};
    success               = StrToU64(&s, &value, &config);
    result                = result && (success && value == 0xABCD);
    StrDeinit(&s);
    s       = StrInitFromZstr("0xABCD");
    success = StrToU64(&s, &value, NULL);
    result  = result && (success && value == 0xABCD);
    StrDeinit(&s);
    s       = StrInitFromZstr("0b101010");
    success = StrToU64(&s, &value, NULL);
    result  = result && (success && value == 42);
    StrDeinit(&s);
    s       = StrInitFromZstr("0o52");
    success = StrToU64(&s, &value, NULL);
    result  = result && (success && value == 42);
    StrDeinit(&s);
    s       = StrInitFromZstr("0");
    success = StrToU64(&s, &value, NULL);
    result  = result && (success && value == 0);
    StrDeinit(&s);
    s       = StrInitFromZstr("not a number");
    success = StrToU64(&s, &value, NULL);
    result  = result && (!success);
    StrDeinit(&s);
    s       = StrInitFromZstr("-123");
    success = StrToU64(&s, &value, NULL);
    result  = result && (!success);
    StrFromU64(&s, u64_values[i], &config);
    u64  recovered_u64 = 0;
    bool success       = StrToU64(&s, &recovered_u64, NULL);
    result             = result && success && (recovered_u64 == u64_values[i]);
    StrFromU64(&s, u64_values[i], &config);
    recovered_u64 = 0;
    success       = StrToU64(&s, &recovered_u64, NULL); // Can now use explicit base 16 with 0x prefix
    result        = result && success && (recovered_u64 == u64_values[i]);
    // For bases with prefixes, use auto-detect. For others, use explicit base parsing
    StrParseConfig parse_config = {.base = base};
    bool           success      = StrToU64(&s, &recovered, config.use_prefix ? NULL : &parse_config);
    result                      = result && success && (recovered == base);
    StrFromU64(&s, UINT64_MAX, &config);
    u64  recovered_max = 0;
    bool success       = StrToU64(&s, &recovered_max, NULL);
    result             = result && success && (recovered_max == UINT64_MAX);
    u64            value    = 0;
    StrParseConfig config   = {.base = prefix_tests[i].base};
    bool           success  = StrToU64(&test_str, &value, prefix_tests[i].base == 0 ? NULL : &config);
    result                  = result && success && (value == prefix_tests[i].expected);
    StrDeinit(&test_str);
    u64            recovered = 0;
    StrParseConfig pconfig   = {.base = base};
    bool           success   = StrToU64(&s, &recovered, &pconfig); // Can now use explicit base with prefixes
    result                   = result && success && (recovered == large_value);
    u64            recovered    = 0;
    StrParseConfig parse_config = {.base = base};
    bool           success      = StrToU64(&s, &recovered, &parse_config);
    
    result = result && success && (recovered == test_value);
    u64            recovered    = 0;
    StrParseConfig parse_config = {.base = base};
    bool           success      = StrToU64(&s, &recovered, &parse_config);
    
    result = result && success && (recovered == test_value);
    u64            recovered    = 0;
    StrParseConfig parse_config = {.base = base};
    bool           success      = StrToU64(&s, &recovered, &parse_config);
    
    result = result && success && (recovered == test_values[i]);
    
    u64  recovered = 0;
    bool success   = StrToU64(&s, &recovered, NULL);
    result         = result && success && (recovered == test_value);
    Str  long_str   = StrInitFromZstr(long_number);
    u64  long_value = 0;
    bool success    = StrToU64(&long_str, &long_value, NULL);
    // This might overflow, but should handle gracefully
    result = result && (success || !success); // Either succeeds or fails gracefully

Share :