StrToI64

Table of Contents

StrToI64

Description

Convert string to signed 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
    i64 val;
    if (!StrToI64(&temp, &val, NULL)) {
    LOG_ERROR("Failed to parse i8");
    StrDeinit(&temp);
    // Use base 0 to let strtoul detect the base from prefix
    i64 val;
    if (!StrToI64(&temp, &val, NULL)) {
    LOG_ERROR("Failed to parse i16");
    StrDeinit(&temp);
    // Use base 0 to let strtoul detect the base from prefix
    i64 val;
    if (!StrToI64(&temp, &val, NULL)) {
    LOG_ERROR("Failed to parse i32");
    StrDeinit(&temp);
    
    // Use base 0 to let strtoul detect the base from prefix
    if (!StrToI64(&temp, v, NULL)) {
    LOG_ERROR("Failed to parse i64");
    StrDeinit(&temp);
    }
    
    bool StrToI64(const Str* str, i64* value, const StrParseConfig* config) {
    ValidateStr(str);
    // Test StrToI64 function
    bool test_str_to_i64(void) {
    printf("Testing StrToI64\n");
    
    // Test positive decimal conversion
    Str  s       = StrInitFromZstr("12345");
    i64  value   = 0;
    bool success = StrToI64(&s, &value, NULL);
    bool result  = (success && value == 12345);
    StrDeinit(&s);
    s       = StrInitFromZstr("-12345");
    success = StrToI64(&s, &value, NULL);
    result  = result && (success && value == -12345);
    StrDeinit(&s);
    s       = StrInitFromZstr("0xABCD");
    success = StrToI64(&s, &value, NULL);
    result  = result && (success && value == 0xABCD);
    StrDeinit(&s);
    s       = StrInitFromZstr("0b101010");
    success = StrToI64(&s, &value, NULL);
    result  = result && (success && value == 42);
    StrDeinit(&s);
    s       = StrInitFromZstr("0");
    success = StrToI64(&s, &value, NULL);
    result  = result && (success && value == 0);
    StrDeinit(&s);
    s       = StrInitFromZstr("not a number");
    success = StrToI64(&s, &value, NULL);
    result  = result && (!success);
    StrFromI64(&s, i64_values[i], &config);
    i64  recovered_i64 = 0;
    bool success       = StrToI64(&s, &recovered_i64, NULL);
    result             = result && success && (recovered_i64 == i64_values[i]);
    StrFromI64(&s, INT64_MIN + 1, &config);
    i64 recovered_min = 0;
    success           = StrToI64(&s, &recovered_min, NULL);
    result            = result && success && (recovered_min == INT64_MIN + 1);

Share :