StrToU64
- Function
- August 22, 2025
Table of Contents
StrToU64
StrToU64
Description
Convert string to unsigned 64-bit integer
Parameters
Name | Direction | Description |
---|---|---|
str | in | String to convert |
value | out | Where to store the result |
config | in | Parse configuration (NULL for auto-detect base) |
Success
Returns true and stores result in value
Failure
Returns false if conversion fails
Usage example (Cross-references)
- In
Io.c:1676
:
// 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);
- In
Io.c:1751
:
// 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);
- In
Io.c:1825
:
// 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);
- In
Io.c:1899
:
// Use base 0 to let strtoul detect the base from prefix
if (!StrToU64(&temp, v, NULL)) {
LOG_ERROR("Failed to parse u64");
StrDeinit(&temp);
- In
Io.c:2328
:
u64 value;
StrParseConfig config = {.base = 16};
if (!StrToU64(&hex_str, &value, &config)) {
LOG_ERROR("Failed to parse hex value");
StrDeinit(&hex_str);
- In
Io.c:2364
:
u64 value;
StrParseConfig config = {.base = 8};
if (!StrToU64(&oct_str, &value, &config)) {
LOG_ERROR("Failed to parse octal value");
StrDeinit(&oct_str);
- In
Str.c:567
:
}
bool StrToU64(const Str* str, u64* value, const StrParseConfig* config) {
ValidateStr(str);
- In
Str.c:699
:
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