Giter Club home page Giter Club logo

radix's Introduction

Radix

Website · Report Issue · Source Code     Build Status Gem Version     Flattr Me

Radix is a very easy to use Ruby library for converting numbers to and from any base. It supports both 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 Integers, Floats and Rational numbers.
  • Define custom encoding 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.

255.to_s(16)   #=> "FF"

"FF".to_i(16)  #=> 255

But Ruby reaches it's limit at base 36.

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.

[100,10].b(256).to_a(10)  #=> [2,5,6,1,0]

Or, to get a string representation for any base up to 62.

[100,10].b(256).to_s(10)  #=> "25610"

A string representation of a number can be converted too, again, up to base 62.

"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.

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.

Installing

To install with RubyGems simply open a console and type:

$ gem install radix

Radix follows Ruby Setup package standard so it can also be installed in an FHS compliant manner using setup.rb.

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

This program is distributable in accordance with the BSD-2-Clause license.

See LICENSE.txt for details.

radix's People

Contributors

trans avatar douglascodes avatar phlipper avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.