Trait ToBytes

trait ToBytes

Associated Types

type Bytes: TraitBound { trait_: Path { path: "NumBytes", id: Id(1880), args: None }, generic_params: [], modifier: None }

Required Methods

fn to_be_bytes(self: &Self) -> <Self as >::Bytes

Return the memory representation of this number as a byte array in big-endian byte order.

Examples

use num_traits::ToBytes;

let bytes = ToBytes::to_be_bytes(&0x12345678u32);
assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
fn to_le_bytes(self: &Self) -> <Self as >::Bytes

Return the memory representation of this number as a byte array in little-endian byte order.

Examples

use num_traits::ToBytes;

let bytes = ToBytes::to_le_bytes(&0x12345678u32);
assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);

Provided Methods

fn to_ne_bytes(self: &Self) -> <Self as >::Bytes

Return the memory representation of this number as a byte array in native byte order.

As the target platform's native endianness is used, portable code should use to_be_bytes or to_le_bytes, as appropriate, instead.

Examples

use num_traits::ToBytes;

#[cfg(target_endian = "big")]
let expected = [0x12, 0x34, 0x56, 0x78];

#[cfg(target_endian = "little")]
let expected = [0x78, 0x56, 0x34, 0x12];

let bytes = ToBytes::to_ne_bytes(&0x12345678u32);
assert_eq!(bytes, expected)

Implementors