Skip to content

IntFromBinary

Description

Compatibility wrapper for IntTryFromBinary(...).

Success

Returns Parsed integer value, or zero on failure.

Failure

Returns a zero-initialised Int on a non-binary character, an empty digit run, or an allocation failure. Use IntTryFromBinary(...) when explicit failure propagation is required.

Usage example (Cross-references)

Usage examples (Cross-references)
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromBinary("00101000", &alloc.base);
    
        bool result = IntBitLength(&value) == 6;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromBinary("0001001000110100", &alloc.base);
    
        bool result = IntByteLength(&value) == 2;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromBinary("1010000", &alloc.base);
        Int zero  = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromBinary("001011", ALLOCATOR_OF(&alloc));
        Str text  = IntToBinary(&value);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int lhs = IntFromBinary("0001011", ALLOCATOR_OF(&alloc));
        Int rhs = IntFrom(11, ALLOCATOR_OF(&alloc));
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  zero  = IntFromBinary("0", ALLOCATOR_OF(&alloc));
        Str  text  = IntToBinary(&zero);
        bool error = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromBinary("0b1010_0011", ALLOCATOR_OF(&alloc));
    
        bool result = IntToU64(&value) == 163;
    
    bool test_int_from_binary_invalid_digit(void) {
        WriteFmt("Testing IntFromBinary invalid digit handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  parsed = IntFromBinary("10a1", ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromBinary(&value, "10a1");
    
    bool test_int_from_binary_null(void) {
        WriteFmt("Testing IntFromBinary NULL handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int  parsed = IntFromBinary((Zstr)NULL, ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool result = !IntTryFromBinary(&value, (Zstr)NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromBinary("101101", &alloc.base);
    
        IntClear(&value);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int original = IntFromBinary("1011", &alloc.base);
        Int clone    = IntClone(&original);
        Int big_dec = IntFromStr("123456789012345678901234567890", alloc_base);
        Int hex_val = IntFromHexStr("deadbeefcafebabe1234", alloc_base);
        Int bin_val = IntFromBinary("10100011", alloc_base);
        Int oct_val = IntFrom(493, alloc_base);
        Int a = IntFrom(41, &alloc.base);
        Int b = IntFrom(42, &alloc.base);
        Int c = IntFromBinary("000101010", &alloc.base);
    
        bool result = IntCompare(&a, &b) < 0;
        Int a = IntFrom(41, &alloc.base);
        Int b = IntFrom(42, &alloc.base);
        Int c = IntFromBinary("000101010", &alloc.base);
    
        bool result = IntLT(&a, &b);
    
        Int value = IntFrom(42, &alloc.base);
        Int same  = IntFromBinary("00101010", &alloc.base);
        Int big   = IntFrom(1, &alloc.base);
    
        Int a = IntFrom(42u, &alloc.base);
        Int b = IntFromBinary("101010", &alloc.base);
        Int c = IntFrom(0u, &alloc.base);
        Int d = IntFrom(0u, &alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromBinary("110000", &alloc.base);
    
        IntShiftRight(&value, 4);
Last updated on