Skip to content
IntTryFromHexStr

IntTryFromHexStr

Description

Parse a hexadecimal string into an integer. This parser expects hexadecimal digits only and does not accept a 0x prefix. Underscore separators are NOT skipped (radix 16 path does not allow underscores).

Parameters

Name Direction Description
out in,out Int to overwrite with the parsed value. Must already be a valid initialised Int; its allocator is reused for the parsed result.
hex in Null-terminated string of hexadecimal digits (0-9, a-f, A-F), with no base prefix.

Success

Returns true. *out is deinitialised and replaced with the normalised parsed value.

Failure

Returns false on a non-hex character (including underscore or a 0x prefix), an empty digit run, or an allocation failure during accumulation. *out retains its pre-call value.

Usage example (Cross-references)

Usage examples (Cross-references)
        Int  parsed = IntFromHexStr("12g3", ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromHexStr(&value, "12g3");
    
        result = result && IntIsZero(&parsed);
    
    bool test_int_try_from_hex_null(void) {
        WriteFmt("Testing IntTryFromHexStr NULL handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntInit(ALLOCATOR_OF(&alloc));
        IntTryFromHexStr(&value, (Zstr)NULL);
        IntDeinit(&value);
        DefaultAllocatorDeinit(&alloc);
Last updated on