pub trait IntoAscii {
// Required methods
fn digits10(self) -> usize;
fn int_to_bytes(self, buff: &mut [u8]);
// Provided method
fn itoa(&self) -> Vec<u8> ⓘ
where Self: Copy { ... }
}Expand description
This trait converts integers to bytes.
Required Methods§
Sourcefn digits10(self) -> usize
fn digits10(self) -> usize
Returns the size of an integer. This is how many digits the integer has.
Sourcefn int_to_bytes(self, buff: &mut [u8])
fn int_to_bytes(self, buff: &mut [u8])
Performs the actual convertion. It fills the given buff with bytes.
Due to how the algorithm works, the last elements of the buffer get written to first.
The buffer should have a size such that it can hold all the bytes.
To get the size of an integer would take, use digits10().
§Examples
extern crate hypernums;
use hypernums::convert::IntoAscii;
fn main() {
let mut v = vec![0, 0, 0, 0, 0];
12345u32.int_to_bytes(&mut v);
assert_eq!(v, [b'1', b'2', b'3', b'4', b'5']);
54321u64.int_to_bytes(&mut v);
assert_eq!(v, [b'5', b'4', b'3', b'2', b'1']);
// if the buffer is larger than the number of digits, it fills with 0.
123u8.int_to_bytes(&mut v);
assert_eq!(v, [b'0', b'0', b'1', b'2', b'3']);
// use slicing to collect 2 numbers into the buffer:
12u8.int_to_bytes(&mut v[..2]);
648u32.int_to_bytes(&mut v[2..]);
assert_eq!(v, [b'1', b'2', b'6', b'4', b'8']);
}