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;
Last updated on