Skip to content

IntToBytesLE

Description

Export an integer into little-endian bytes.

Parameters

Name Direction Description
value in Integer to export.
bytes out Destination byte buffer. Must be non-NULL and at least max_len bytes long.
max_len in Maximum bytes to write. Must be non-zero.

Success

Returns the number of bytes written, which is min(IntByteLength(value), max_len). The first byte written holds the least significant 8 bits. value is not modified.

Failure

Returns 0 when value has zero bytes of magnitude. LOG_FATAL when bytes is NULL or max_len is 0.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    u64 IntToBytesLE(const Int *value, u8 *bytes, u64 max_len) {
        ValidateInt(value);
        u8  out[4]  = {0};
        Int value   = IntFromBytesLE(bytes, sizeof(bytes), ALLOCATOR_OF(&alloc));
        u64 written = IntToBytesLE(&value, out, sizeof(out));
        Str text    = IntToHexStr(&value);
    
    bool test_int_to_bytes_le_null(void) {
        WriteFmt("Testing IntToBytesLE NULL handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int value = IntFrom(1, ALLOCATOR_OF(&alloc));
        IntToBytesLE(&value, NULL, 1);
        DefaultAllocatorDeinit(&alloc);
        return false;
    //
    bool test_m21_to_bytes_le_count(void) {
        WriteFmt("Testing IntToBytesLE writes exact byte count\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value     = IntFrom(0x1234, ALLOCATOR_OF(&alloc));
        u8  out[8]    = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
        u64 written   = IntToBytesLE(&value, out, sizeof(out));
        u8  expect[2] = {0x34, 0x12};
    //
    bool test_m21_to_bytes_le_single(void) {
        WriteFmt("Testing IntToBytesLE single-byte value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value   = IntFrom(1, ALLOCATOR_OF(&alloc));
        u8  out[4]  = {0xAA, 0xAA, 0xAA, 0xAA};
        u64 written = IntToBytesLE(&value, out, sizeof(out));
    
        bool result = (written == 1);
Last updated on