Skip to content

IntFromBinary

IntFromBinary

Description

Parse a binary string into an integer. Accepts an optional 0b or 0B prefix.

Parameters

Name Direction Description
binary in Binary digit string

Usage example (from documentation)

  Int value = IntFromBinary("0b101101");

Returns

Parsed integer value.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    Int IntFromBinary(const char *binary) {
        if (!binary) {
            LOG_FATAL("binary is NULL");
        WriteFmt("Testing Int binary round trip\n");
    
        Int value = IntFromBinary("001011");
        Str text  = IntToBinary(&value);
        WriteFmt("Testing IntCompare leading-zero normalization\n");
    
        Int lhs = IntFromBinary("0001011");
        Int rhs = IntFrom(11);
        WriteFmt("Testing Int zero binary conversion\n");
    
        Int zero = IntFromBinary("0");
        Str text = IntToBinary(&zero);
        WriteFmt("Testing Int binary prefix and separators\n");
    
        Int value = IntFromBinary("0b1010_0011");
    
        bool result = IntToU64(&value) == 163;
    
    bool test_int_from_binary_invalid_digit(void) {
        WriteFmt("Testing IntFromBinary invalid digit handling\n");
    
        IntFromBinary("10a1");
        WriteFmt("Testing IntFromBinary invalid digit handling\n");
    
        IntFromBinary("10a1");
        return false;
    }
    
    bool test_int_from_binary_null(void) {
        WriteFmt("Testing IntFromBinary NULL handling\n");
    
        IntFromBinary(NULL);
        WriteFmt("Testing IntFromBinary NULL handling\n");
    
        IntFromBinary(NULL);
        return false;
    }
        Int a = IntFrom(41);
        Int b = IntFrom(42);
        Int c = IntFromBinary("000101010");
    
        bool result = IntCompare(&a, &b) < 0;
        Int a = IntFrom(41);
        Int b = IntFrom(42);
        Int c = IntFromBinary("000101010");
    
        bool result = IntLT(&a, &b);
    
        Int value = IntFrom(42);
        Int same  = IntFromBinary("00101010");
        Int big   = IntFrom(1);
        Int big_dec = IntFromStr("123456789012345678901234567890");
        Int hex_val = IntFromHexStr("deadbeefcafebabe1234");
        Int bin_val = IntFromBinary("10100011");
        Int oct_val = IntFrom(493);
        WriteFmt("Testing IntBitLength\n");
    
        Int value = IntFromBinary("00101000");
    
        bool result = IntBitLength(&value) == 6;
        WriteFmt("Testing IntByteLength\n");
    
        Int value = IntFromBinary("0001001000110100");
    
        bool result = IntByteLength(&value) == 2;
        WriteFmt("Testing IntTrailingZeroCount\n");
    
        Int value = IntFromBinary("1010000");
        Int zero  = IntInit();
        WriteFmt("Testing IntShiftRight\n");
    
        Int value = IntFromBinary("110000");
    
        IntShiftRight(&value, 4);
        WriteFmt("Testing IntClear\n");
    
        Int value = IntFromBinary("101101");
    
        IntClear(&value);
        WriteFmt("Testing IntClone\n");
    
        Int original = IntFromBinary("1011");
        Int clone    = IntClone(&original);
Last updated on