https://github.com/rubyworks/radix
Base Conversions
https://github.com/rubyworks/radix
Last synced: 6 months ago
JSON representation
Base Conversions
- Host: GitHub
- URL: https://github.com/rubyworks/radix
- Owner: rubyworks
- License: other
- Created: 2009-09-08T22:16:39.000Z (over 16 years ago)
- Default Branch: master
- Last Pushed: 2016-08-21T08:48:25.000Z (over 9 years ago)
- Last Synced: 2024-11-30T14:39:48.012Z (about 1 year ago)
- Language: Ruby
- Homepage: http://rubyworks.github.com/radix
- Size: 1.03 MB
- Stars: 54
- Watchers: 3
- Forks: 9
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Radix
[](https://rubygems.org/gems/radix)
[](http://github.com/rubyworks/radix)
[](http://github.com/rubyworks/radix/issues)
[](http://travis-ci.org/rubyworks/radix)
[](https://www.gittip.com/on/github/rubyworks/)
[](http://flattr.com/thing/324911/Rubyworks-Ruby-Development-Fund)
[Radix]((http://rubyworks.github.com/radix)) is a very easy to use Ruby library
for converting numbers to and from any base. It supports Integer, Float and
Rational numbers, as well as representational string-notations that need not be
in ASCII order.
## Features
* Convert to and from any base.
* Convert Integer, Float and Rational numbers.
* Define custom encodings and character sets.
* Can be used to encode/decode bytecode strings.
* Very intuitive API.
## Usage
Base conversions with ASCII ordered notations are easy in Ruby.
```ruby
255.to_s(16) #=> "FF"
"FF".to_i(16) #=> 255
```
But Ruby reaches it's limit at base 36.
```ruby
255.to_s(37) #=> Error
```
Radix provides the means of converting to and from any base.
For example, a number in base 256 can be represented by the array `[100, 10]`
(`100**256 + 10**1`) and can be convert to base 10.
```ruby
[100,10].b(256).to_a(10) #=> [2,5,6,1,0]
```
Or, to get a string representation for any base up to 62.
```ruby
[100,10].b(256).to_s(10) #=> "25610"
```
A string representation of a number can be converted too, again,
up to base 62.
```ruby
"10".b(62).to_s(10) #=> "62"
```
To use a custom character set, use an array of characters as the base
rather than an integer. For example we can convert a base 10 number
to another base 10 number using a different encoding.
```ruby
base = [:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U]
"10".b(10).to_a(base) #=> [:W, :Q]
```
To learn more have a look at the [QED Demo](http://rubydoc.info/gems/radix/file/DEMO.md).
## Installing
If using Bundler, then add the ususal gem entry to your project's Gemfile.
gem 'radix'
To install with RubyGems simply open a console and type:
$ gem install radix
Radix follows [Ruby Setup](http://rubyworks.github.com/setup) package standard
so it can also be installed in an FHS compliant manner using setup.rb (very
old-school and no longer recommeded).
## Special Thanks
Special thanks to **douglascodes** for taking the time to fully document
Radix's API. Documentation is an under-addressed and time-consuming affair,
so your contribution is greatly appreciated. Thank you, Douglas!
## Copyrights
Copyright (c) 2009 [Rubyworks](https://rubyworks.github.io)
This program is distributable in accordance with the *BSD-2-Clause* license.
See LICENSE.txt for details.