Skip to content

IntToBinary

Description

Convert an integer to a binary string.

Parameters

Name Direction Description
value in Integer to convert.

Success

Returns a Str holding the base-2 textual form of value, bound to value’s allocator. value is not modified.

Failure

Returns an empty Str bound to value’s allocator when the underlying IntToStrRadix fails (intermediate allocation failure). The caller cannot distinguish that from a true empty result; use IntTryToStrRadix directly when failure detection is required.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    Str IntToBinary(const Int *value) {
        return IntToStrRadix(value, 2, false);
    }
    
        Int value = IntFrom(13, ALLOCATOR_OF(&alloc));
        Str text  = IntToBinary(&value);
    
        bool result = IntBitLength(&value) == 4;
    
        Int value = IntFromBinary("001011", ALLOCATOR_OF(&alloc));
        Str text  = IntToBinary(&value);
    
        bool result = IntToU64(&value) == 11;
    
        Int  zero  = IntFromBinary("0", ALLOCATOR_OF(&alloc));
        Str  text  = IntToBinary(&zero);
        bool error = true;
        z = "10100011";
        StrReadFmt(z, "{b}", bin);
        bin_text = IntToBinary(&bin);
        success  = success && (ZstrCompare(StrBegin(&bin_text), "10100011") == 0);
    
        IntAdd(&result_value, &a, &b);
        text = IntToBinary(&result_value);
    
        bool result = IntToU64(&result_value) == 256;
Last updated on