#unsigned-integer #packing #u8 #u32 #size #different #byte-packing

intpack

intpack is a collection of functions for packing/unpacking unsigned integers into other unsigned integers of different sizes. For example, packing 4 u8's into a u32.

1 unstable release

0.1.0 Sep 28, 2021

#27 in #u32

MIT license

8KB
119 lines

intpack

intpack is a collection of functions for packing/unpacking unsigned integers into other unsigned integers of different sizes. For example, packing 4 u8's into a u32.

!! The functions haven't been fully tested (but they should work) !!

Usage

Packing 4 u8's into a u32 using the u8_to_u32 function:

use intpack::pack;

let result = pack::u8_to_u32(&[0xff, 0x00, 0xff, 0x00]);
// Returns 0xff00ff00

And unpacking that u32:

use intpack::unpack;

let result = unpack::u32_to_u8(0xff00ff00);
// Returns [0xff, 0x00, 0xff, 0x00]

It's important to note, when packing, the least significant byte in the input slice (at index 0) becomes the most significant byte in the output value.

When unpacking, the most significant byte in the input value becomes the least significant byte in the output array (at index 0).


lib.rs:

intpack is a collection of functions for packing/unpacking unsigned integers into other unsigned integers of different sizes. For example, packing 4 u8's into a u32.

No runtime deps