Skip to content

moret/num_coder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NumCoder

A simple numbers to string encoder/decoder gem, Base64 style.

It takes a number - or a list of numbers - and encodes it to the selected charset. To do so, it changes de number from base 10 to base-size-of-selected-charset. Meaning you can get a large integer represented in less characters than it would if you used a straight decimal representation. Useful for those places where you only have support for strings.

Setup and usage example

First, get the gem:

$ gem install num_coder

Or place it on your Gemspec and bundle install it. Now, fire up irb and load num_coder:

$ irb
> require "num_coder"

Now, test some examples using the included charsets.

Number to Base95 (all readable ASCII charaters) string and back:

> NumCoder.encode95 1234567890
=> "/.y5M"
> NumCoder.decode95 "/.y5M"
=> 1234567890

List of numbers to Base95 and back:

> NumCoder.fixed_encode95 [1234567890,1876543290,6758493021], 5
=> "/.y5M7#c69r|iNl"
> NumCoder.fixed_decode95 "/.y5M7#c69r|iNl", 5
=> [1234567890, 1876543290, 6758493021]

Similar on Base64 and Base16 (hexa):

> NumCoder.encode64 1234567890
=> "BJlgLS"
> NumCoder.decode64 "BJlgLS"
=> 1234567890
> NumCoder.encode16 1234567890
=> "499602d2"
> NumCoder.decode16 "499602d2"
=> 1234567890

Oh, and one more thing, you can encode and decode directly using a charset of your own:

> NumCoder.encode "áéíóúãõâêîôû*@!.,[]", 1234567890
=> "éâúûãâ*â"
> NumCoder.encode "0987654321", 1234567890
=> "9876543210"
> NumCoder.decode "0123456789abcdefghijklmnopqrstuvwxyz ", "foo bar"
=> 40196969931

Included charsets

For your convenience. Here they are:

Base95:

> puts NumCoder.cs95
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Base64:

> puts NumCoder.cs64
=> ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

Base16:

> puts NumCoder.cs16
=> 0123456789abcdef

About

A simple number to string encoder/decoder gem, Base64 style.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages