Crates.io | base_custom |
lib.rs | base_custom |
version | 0.2.0 |
source | src |
created_at | 2017-06-22 15:05:03.11408 |
updated_at | 2018-12-07 01:18:00.368239 |
description | Use any characters as your own numeric base and convert to and from decimal. |
homepage | https://github.com/danielpclark/base_custom |
repository | https://github.com/danielpclark/base_custom |
max_upload_size | |
id | 20172 |
size | 31,610 |
Use any characters as your own numeric base and convert to and from decimal. This can be taken advantage of in various ways:
There is also a Ruby and Crystal implementation of this which this was based off of.
Add the following to your Cargo.toml file
[dependencies]
base_custom = "^0.2"
To include it for usage add
extern crate base_custom;
use base_custom::BaseCustom;
to your file.
// Binary with no delimiter
let base2 = BaseCustom::<char>::new("01".chars().collect());
assert_eq!(base2.decimal("00001"), 1_u64);
assert_eq!(base2.decimal("100110101"), 309_u64);
assert_eq!(base2.gen(340), "101010100");
assert_eq!(base2.gen(0xF45), "111101000101");
assert_eq!(base2.gen(0b111), "111");
// Trinary with no delimiter
let base3 = BaseCustom::<char>::new("ABC".chars().collect());
assert_eq!(base3.decimal("ABC"), 5);
assert_eq!(base3.gen(123), "BBBCA");
// Custom base like Musical Chords and a space delimiter
let base_music = BaseCustom::<String>::new("A A# B C C# D D# E F F# G G#", Some(' '));
assert_eq!(base_music.decimal("F F# B D# D A# D# F# "), 314159265);
assert_eq!(base_music.gen(314159265), "F F# B D# D A# D# F# ");
When using BaseCustom::<String>::new
the second parameter must be of Option<char>
to
choose your optional delimiter.
Benchmarks are provided for the various usages and implementations. BaseCustom<char>
is
by far the most efficient implementation.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.