https://github.com/danielpclark/base_custom
Rust implementation of custom numeric base conversion.
https://github.com/danielpclark/base_custom
base-conversion number-converter numeric numerical-calculations
Last synced: 6 months ago
JSON representation
Rust implementation of custom numeric base conversion.
- Host: GitHub
- URL: https://github.com/danielpclark/base_custom
- Owner: danielpclark
- License: mit
- Created: 2017-06-21T20:50:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-07T01:17:49.000Z (about 7 years ago)
- Last Synced: 2024-10-07T15:18:14.533Z (over 1 year ago)
- Topics: base-conversion, number-converter, numeric, numerical-calculations
- Language: Rust
- Homepage:
- Size: 30.3 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# base_custom
[](https://travis-ci.org/danielpclark/base_custom)
[](https://crates.io/crates/base_custom)
[]()
Use any characters as your own numeric base and convert to and from decimal. This can be taken advantage of in various ways:
* Mathematics: number conversion
* Brute force sequencing
* Rolling ciphers
* Moderate information concealment
* Other potential uses such as deriving music or art from numbers
_There is also a Ruby and Crystal implementation of this which this was based off of._
### Installation
Add the following to your Cargo.toml file
```toml
[dependencies]
base_custom = "^0.2"
```
To include it for usage add
```rust
extern crate base_custom;
use base_custom::BaseCustom;
```
to your file.
### Usage
```rust
// Binary with no delimiter
let base2 = BaseCustom::::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::::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::::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::::new` the second parameter must be of `Option` to
choose your optional delimiter.
### Benchmarks
Benchmarks are provided for the various usages and implementations. `BaseCustom` is
by far the most efficient implementation.
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
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.