bytes_ext
Version, currently 0.3.03 versions
- 0.3.0latestJun 23, 2026
- 0.2.0not indexedJun 29, 2026
- 0.1.0not indexedJun 29, 2026
github.com/mamantoha/bytes_ext
Byte conversion helpers for Crystal numeric types
8 stars
0 dependents
License: MIT
Installation
# Add this to your shard.yml
dependencies:
bytes_ext:
github: mamantoha/bytes_ext
version: ~> 0.3.0Then run:
shards installshard.yml
- Crystal
>= 1.0.0- License
- MIT
- Author
- Anton Maminov <anton.maminov@gmail.com>
Dependencies
This version declares no dependencies.
README
# bytes_ext
[](https://github.com/mamantoha/bytes_ext/actions/workflows/crystal.yml)
[](https://github.com/mamantoha/bytes_ext/releases)
[](https://mamantoha.github.io/bytes_ext/)
[](https://github.com/mamantoha/bytes_ext/blob/master/LICENSE)
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`:
```yaml
dependencies:
bytes_ext:
github: mamantoha/bytes_ext
```
Then require it:
```crystal
require "bytes_ext"
```
## API
The following methods are added to supported numeric types:
```crystal
value.to_le_bytes
value.to_be_bytes
value.to_ne_bytes
value.to_bytes
```
The following class methods parse bytes back into values:
```crystal
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
- `Int8`, `Int16`, `Int32`, `Int64`, `Int128`
- `UInt8`, `UInt16`, `UInt32`, `UInt64`, `UInt128`
- `Float32`, `Float64`
## Examples
### Little Endian
```crystal
i32 = 0x12345678_i32
bytes = i32.to_le_bytes
# => Bytes[0x78, 0x56, 0x34, 0x12]
Int32.from_le_bytes(bytes)
# => 0x12345678_i32
```
### Big Endian
```crystal
i32 = 0x12345678_i32
bytes = i32.to_be_bytes
# => Bytes[0x12, 0x34, 0x56, 0x78]
Int32.from_be_bytes(bytes)
# => 0x12345678_i32
```
### Native Endian
```crystal
i32 = 0x12345678_i32
bytes = i32.to_ne_bytes
Int32.from_ne_bytes(bytes)
# => 0x12345678_i32
```
### Explicit Byte Format
```crystal
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
```crystal
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:
```sh
crystal spec
```
## Contributing
1. Fork the repository.
2. Create a feature branch.
3. Make your changes with focused tests.
4. Open a pull request.
## License
This shard is available under the MIT license. See [LICENSE](LICENSE).
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.3.0- Tagged
- Jun 23, 2026
- Commit
6f530bfe3675- Crystal
>= 1.0.0- Indexed
- yes
Dependents
No indexed shard depends on this one yet.
Repository
github.com/mamantoha/bytes_ext
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 12, 2026
- Synced
- Aug 12, 2026
- Versions
- 3