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;
Last updated on