Skip to content
IntFromStrRadix

IntFromStrRadix

Description

Compatibility wrapper for IntTryFromStrRadix(...).

Success

Returns Parsed integer value, or zero on failure.

Failure

Returns a zero-initialised Int on an invalid radix, an invalid digit, an empty digit run, or an allocation failure. Use IntTryFromStrRadix(...) when explicit failure propagation is required.

Usage example (Cross-references)

Usage examples (Cross-references)
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromStrRadix("zz", 36, ALLOCATOR_OF(&alloc));
        Str text  = IntToStrRadix(&value, 36, false);
    
    bool test_int_from_radix_invalid_digit(void) {
        WriteFmt("Testing IntFromStrRadix invalid digit handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  parsed = IntFromStrRadix("102", 2, ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromStrRadix(&value, "102", 2);
    
    bool test_int_from_radix_invalid_radix(void) {
        WriteFmt("Testing IntFromStrRadix invalid radix handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  parsed = IntFromStrRadix("10", 1, ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromStrRadix(&value, "10", 1);
    
    bool test_int_from_radix_null(void) {
        WriteFmt("Testing IntFromStrRadix NULL handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        IntFromStrRadix((Zstr)NULL, 10, ALLOCATOR_OF(&alloc));
        DefaultAllocatorDeinit(&alloc);
        return false;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int parsed_dec = IntFromStrRadix("987654321987654321", 10, &alloc.base);
        Int parsed_hex = IntFromStrRadix("deadbeef", 16, &alloc.base);
    
        Int parsed_dec = IntFromStrRadix("987654321987654321", 10, &alloc.base);
        Int parsed_hex = IntFromStrRadix("deadbeef", 16, &alloc.base);
    
        Str back_dec = IntToStrRadix(&parsed_dec, 10, false, &alloc.base);
    
        Str digits = StrInitFromZstr("ff", &alloc.base);
        Int value  = IntFromStrRadix(&digits, (u8)16u, &alloc.base);
    
        bool result = (IntToU64(&value) == (u64)255u);
        Int value  = IntFromStr("123456789012345678901234567890", alloc);
        Str hex    = IntToStrRadix(&value, 16, false, alloc);
        Int parsed = IntFromStrRadix(&hex, 16, alloc);
    
        bool ok = IntCompare(&value, &parsed) == 0;
Last updated on