#![no_std]
Compatible with Zero Heap AllocationsThe 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.
Both the standard library and itoa crate rely on unsafe functions, but this implementation has been able to avoid the use of unsafe entirely.
Performance is roughly identical to that of the itoa
crate when performing base 10 conversions. Below is a benchmark
of printing 1,000,000 through 5,000,000 to /dev/null
std: 1313015386 ns
numtoa: 805112957 ns
itoa: 799623465 ns
```rust 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 mut startindice = number.numtoa(10, &mut buffer); let _ = stdout.write(&buffer[startindice..]); let _ = stdout.write(b"\n"); asserteq!(&buffer[startindice..], b"162392");
let othernumber: i32 = -6235; startindice = othernumber.numtoa(10, &mut buffer); let _ = stdout.write(&buffer[startindice..]); let _ = stdout.write(b"\n"); asserteq!(&buffer[startindice..], b"-6235");
let largenum: u64 = 35320842; startindice = largenum.numtoa(10, &mut buffer); let _ = stdout.write(&buffer[startindice..]); let _ = stdout.write(b"\n"); asserteq!(&buffer[startindice..], b"35320842");
let maxu64: u64 = 18446744073709551615; startindice = maxu64.numtoa(10, &mut buffer); let _ = stdout.write(&buffer[startindice..]); let _ = stdout.write(b"\n"); asserteq!(&buffer[startindice..], b"18446744073709551615"); ```