Skip to content

Latest commit

 

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

endbyte

crates.io docs build license: MIT no_std downloads

a no_std compatible crate for handling byte order conversions between different endianness formats.

features

  • no_std compatible: usable in embedded environments
  • zero dependencies: no external crates required
  • comprehensive type support: supports integer types (u8, u16, u32, u64, u128, i8, i16, i32, i64, i128)
  • efficient: uses rust's built-in swap_bytes() methods for optimal performance
  • compile-time endianness detection: zero runtime overhead for endianness checks

usage

add this to your Cargo.toml:

[dependencies]
endbyte = "0.1.0"

basic usage

use endbyte::Endianness;

let value = 0x1234u16;

// convert to specific endianness
let big_endian = value.host_to_big_endian();
let little_endian = value.host_to_little_endian();

// convert from specific endianness back to host
let from_big = big_endian.big_endian_to_host();
let from_little = little_endian.little_endian_to_host();

assert_eq!(from_big, value);
assert_eq!(from_little, value);

embedded usage

this library is designed to also work in no_std environments:

#![no_std]

use endbyte::Endianness;

fn process_network_data(data: u32) -> u32 {
    // convert from network byte order (big endian) to host
    let host_value = data.big_endian_to_host();

    // process the value...
    let result = host_value * 2;

    // convert back to network byte order
    result.host_to_big_endian()
}

supported types

the Endianness trait is implemented for all standard integer types:

  • unsigned: u8, u16, u32, u64, u128
  • signed: i8, i16, i32, i64, i128

note: single-byte types (u8, i8) have zero-cost implementations since byte swapping is not needed.

methods

  • host_to_big_endian(): convert from host byte order to big endian
  • host_to_little_endian(): convert from host byte order to little endian
  • big_endian_to_host(): convert from big endian to host byte order
  • little_endian_to_host(): convert from little endian to host byte order

testing

the library includes tests that work on both big and little endian systems:

cargo test

for embedded targets:

cargo build --target thumbv7em-none-eabihf
cargo build --target thumbv6m-none-eabi

contribute

contributions are welcome! please open an issue or pull request if you have any improvements or bug fixes.

license

this project is licensed under the mit license - see the license file for details.

About

Simple no_std compliant conversion from network type to host type and back, for both big and small endian binary formats.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages