Skip to content

IntToStrRadix

Description

Convert an integer to text in the given radix. Two forms via argument count:

  • IntToStrRadix(value, radix, uppercase) - value’s allocator.
  • IntToStrRadix(value, radix, uppercase, alloc) - explicit allocator.

Success

Returns a Str holding the textual form of value in base radix.

Failure

Returns an empty Str bound to alloc when the underlying IntTryToStrRadix fails (unsupported radix or intermediate allocation failure); the caller cannot distinguish that from a true empty result, so callers that need to detect failure should use IntTryToStrRadix directly.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    Str IntToBinary(const Int *value) {
        return IntToStrRadix(value, 2, false);
    }
    
    Str IntToOctStr(const Int *value) {
        return IntToStrRadix(value, 8, false);
    }
    
    Str IntToHexStr(const Int *value) {
        return IntToStrRadix(value, 16, false);
    }
    
        Int value = IntFromStrRadix("zz", 36, ALLOCATOR_OF(&alloc));
        Str text  = IntToStrRadix(&value, 36, false);
    
        bool result = IntToU64(&value) == 1295;
    
        Int value = IntFrom(0xBEEF, ALLOCATOR_OF(&alloc));
        Str text  = IntToStrRadix(&value, 16, true);
    
        bool result = ZstrCompare(StrBegin(&text), "BEEF") == 0;
    
    bool test_int_to_str_radix_invalid_radix(void) {
        WriteFmt("Testing IntToStrRadix invalid radix handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFrom(255, ALLOCATOR_OF(&alloc));
        Str text  = IntToStrRadix(&value, 37, false);
    
        bool result = StrLen(&text) == 0;
        Int big   = IntFromStr("123456789012345678901234567890", &alloc.base);
    
        Str dec_small = IntToStrRadix(&small, 10, false, &alloc.base);
        Str hex_mid   = IntToStrRadix(&mid, 16, false, &alloc.base);
        Str hex_mid_u = IntToStrRadix(&mid, 16, true, &alloc.base);
    
        Str dec_small = IntToStrRadix(&small, 10, false, &alloc.base);
        Str hex_mid   = IntToStrRadix(&mid, 16, false, &alloc.base);
        Str hex_mid_u = IntToStrRadix(&mid, 16, true, &alloc.base);
        Str dec_big   = IntToStrRadix(&big, 10, false, &alloc.base);
        Str dec_small = IntToStrRadix(&small, 10, false, &alloc.base);
        Str hex_mid   = IntToStrRadix(&mid, 16, false, &alloc.base);
        Str hex_mid_u = IntToStrRadix(&mid, 16, true, &alloc.base);
        Str dec_big   = IntToStrRadix(&big, 10, false, &alloc.base);
        Str hex_mid   = IntToStrRadix(&mid, 16, false, &alloc.base);
        Str hex_mid_u = IntToStrRadix(&mid, 16, true, &alloc.base);
        Str dec_big   = IntToStrRadix(&big, 10, false, &alloc.base);
    
        bool result = (ZstrCompare(StrBegin(&dec_small), "7") == 0);
        Int parsed_hex = IntFromStrRadix("deadbeef", 16, &alloc.base);
    
        Str back_dec = IntToStrRadix(&parsed_dec, 10, false, &alloc.base);
        Str back_hex = IntToStrRadix(&parsed_hex, 16, false, &alloc.base);
    
        Str back_dec = IntToStrRadix(&parsed_dec, 10, false, &alloc.base);
        Str back_hex = IntToStrRadix(&parsed_hex, 16, false, &alloc.base);
    
        bool result = (ZstrCompare(StrBegin(&back_dec), "987654321987654321") == 0);
        // (1583/1584) and int_mod_u64 (1630).
        Int  v   = IntFrom(1234567u, a);
        Str  s   = IntToStrRadix(&v, 10, false, a);
        bool ok  = StrLen(&s) == 7;
        u64  rem = 0;
    
        Int value  = IntFromStr("123456789012345678901234567890", alloc);
        Str hex    = IntToStrRadix(&value, 16, false, alloc);
        Int parsed = IntFromStrRadix(&hex, 16, alloc);
Last updated on