bytes_ext
Byte conversion helpers for Crystal numeric types.
bytes_ext adds Rust-style byte conversion methods to integers and floats, making it easy to convert values to and from little-endian, big-endian, and native-endian byte arrays.
Installation
Add the shard to your application's shard.yml:
dependencies:
bytes_ext:
github: mamantoha/bytes_ext
Then require it:
require "bytes_ext"
API
The following methods are added to supported numeric types:
value.to_le_bytes
value.to_be_bytes
value.to_ne_bytes
value.to_bytes
The following class methods parse bytes back into values:
Int32.from_le_bytes(bytes)
Int32.from_be_bytes(bytes)
Int32.from_ne_bytes(bytes)
Int32.from_bytes(bytes)
to_ne_bytes and from_ne_bytes use native-endian byte order. to_bytes and from_bytes are native-endian by default, and also accept an explicit IO::ByteFormat.
Supported Types
Examples
Little Endian
i32 = 0x12345678_i32
bytes = i32.to_le_bytes
# => Bytes[0x78, 0x56, 0x34, 0x12]
Int32.from_le_bytes(bytes)
# => 0x12345678_i32
Big Endian
i32 = 0x12345678_i32
bytes = i32.to_be_bytes
# => Bytes[0x12, 0x34, 0x56, 0x78]
Int32.from_be_bytes(bytes)
# => 0x12345678_i32
Native Endian
i32 = 0x12345678_i32
bytes = i32.to_ne_bytes
Int32.from_ne_bytes(bytes)
# => 0x12345678_i32
Explicit Byte Format
i32 = 0x12345678_i32
bytes = i32.to_bytes(IO::ByteFormat::BigEndian)
# => Bytes[0x12, 0x34, 0x56, 0x78]
Int32.from_bytes(bytes, IO::ByteFormat::BigEndian)
# => 0x12345678_i32
Floats
f32 = 12.5_f32
bytes = f32.to_le_bytes
# => Bytes[0x00, 0x00, 0x48, 0x41]
Float32.from_le_bytes(bytes)
# => 12.5_f32
Development
Run the test suite with:
crystal spec
Contributing
- Fork the repository.
- Create a feature branch.
- Make your changes with focused tests.
- Open a pull request.
License
This shard is available under the MIT license. See LICENSE.