Crate numtoa

Source
Expand description

The standard library provides a convenient method of converting numbers into strings, but these strings are heap-allocated. If you have an application which needs to convert large volumes of numbers into strings, but don’t want to pay the price of heap allocation, this crate provides an efficient no_std-compatible method of heaplessly converting numbers into their string representations, storing the representation within a reusable byte array.

In addition to supporting the standard base 10 conversion, this implementation allows you to select the base of your choice. Therefore, if you want a binary representation, set the base to 2. If you want hexadecimal, set the base to 16.

§Convenience Example

use numtoa::NumToA;

let mut buf = [0u8; 20];
let mut string = String::new();

for number in (1..10) {
    string.push_str(number.numtoa_str(10, &mut buf));
    string.push('\n');
}

println!("{}", string);

§Base 10 Example

use numtoa::NumToA;
use std::io::{self, Write};

let stdout = io::stdout();
let mut stdout = stdout.lock();
let mut buffer = [0u8; 20];

let number: u32 = 162392;
let _ = stdout.write(number.numtoa(10, &mut buffer));
let _ = stdout.write(b"\n");
assert_eq!(number.numtoa(10, &mut buffer), b"162392");

let number: i32 = -6235;
let _ = stdout.write(number.numtoa(10, &mut buffer));
let _ = stdout.write(b"\n");
assert_eq!(number.numtoa(10, &mut buffer), b"-6235");

let number: i8 = -128;
let _ = stdout.write(number.numtoa(10, &mut buffer));
let _ = stdout.write(b"\n");
assert_eq!(number.numtoa(10, &mut buffer), b"-128");

let number: i8 = 53;
let _ = stdout.write(number.numtoa(10, &mut buffer));
let _ = stdout.write(b"\n");
assert_eq!(number.numtoa(10, &mut buffer), b"53");

let number: i16 = -256;
let _ = stdout.write(number.numtoa(10, &mut buffer));
let _ = stdout.write(b"\n");
assert_eq!(number.numtoa(10, &mut buffer), b"-256");

let number: i16 = -32768;
let _ = stdout.write(number.numtoa(10, &mut buffer));
let _ = stdout.write(b"\n");
assert_eq!(number.numtoa(10, &mut buffer), b"-32768");

let number: u64 = 35320842;
let _ = stdout.write(number.numtoa(10, &mut buffer));
let _ = stdout.write(b"\n");
assert_eq!(number.numtoa(10, &mut buffer), b"35320842");

let number: u64 = 18446744073709551615;
let _ = stdout.write(number.numtoa(10, &mut buffer));
let _ = stdout.write(b"\n");
assert_eq!(number.numtoa(10, &mut buffer), b"18446744073709551615");

Modules§

base2
base8
base10
base16

Structs§

AsciiNumber
The result of a number conversion to ascii containing a string with at most length N bytes/characters

Traits§

NumToA
Converts a number into a string representation, storing the conversion into a mutable byte slice.

Functions§

numtoa_i8
numtoa_i8_str
numtoa_i16
numtoa_i32
numtoa_i64
numtoa_i16_str
numtoa_i32_str
numtoa_i64_str
numtoa_i128
numtoa_i128_str
numtoa_isize
numtoa_isize_str
numtoa_u8
numtoa_u8_str
numtoa_u16
numtoa_u32
numtoa_u64
numtoa_u16_str
numtoa_u32_str
numtoa_u64_str
numtoa_u128
numtoa_u128_str
numtoa_usize
numtoa_usize_str