Skip to content
IntFromBytesBE

IntFromBytesBE

Description

Create an integer from big-endian bytes.

Parameters

Name Direction Description
bytes in Source byte buffer holding the magnitude in big-endian order (most significant byte first). May be NULL when len == 0.
len in Number of bytes to read from bytes.
alloc in Allocator to bind to the returned integer.

Success

Returns a normalised Int holding the unsigned magnitude packed in bytes. bytes is not modified.

Failure

Returns a zero-initialised Int bound to alloc when an intermediate shift or add allocation fails. The caller cannot distinguish that from a true zero result. LOG_FATAL when bytes is NULL and len != 0.

Usage example (Cross-references)

Usage examples (Cross-references)
        u8  bytes[] = {0x12, 0x34, 0x56, 0x78};
        u8  out[4]  = {0};
        Int value   = IntFromBytesBE(bytes, sizeof(bytes), ALLOCATOR_OF(&alloc));
        u64 written = IntToBytesBE(&value, out, sizeof(out));
        Str text    = IntToHexStr(&value);
    // ---------------------------------------------------------------------------
    bool test_m24_from_bytes_be_null_zero(void) {
        WriteFmt("Testing IntFromBytesBE(NULL, 0) -> 0\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFromBytesBE((const u8 *)0, 0, &alloc.base);
    
        bool fail = (IntCompare(&value, 0u) != 0);
    // constructed magnitude is exact.
    bool test_m24_from_bytes_be_roundtrip(void) {
        WriteFmt("Testing IntFromBytesBE big-endian accumulation\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        u8  bytes[3] = {0x01, 0x02, 0x03};
        Int value    = IntFromBytesBE(bytes, sizeof(bytes), &alloc.base);
    
        bool fail = (IntCompare(&value, 0x010203u) != 0);
    
        u8  bytes[] = {0x01, 0x02, 0x03, 0x04};
        Int v       = IntFromBytesBE(bytes, sizeof(bytes), &dbg);
    
        bool ok = IntToU64(&v) == 0x01020304u;
Last updated on