Sha256: e5c7555ada689fa78e209fe4b9f7468de71f90e1040a41daa52f14af50cd2f5c

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

=Anybase

==Arbitrary number bases

So often, I need to create random number bases, with random digits, to satisfy some crazy requirement.

Here is an example of how you'd do hex in Anybase.

  base = Anybase.new('0123456789abcdef')
  base.to_i('f0a')
  => 3850
  base.to_native(123450)
  => "1e23a"

As well, you can tell Anybase to ignore case.

  base = Anybase.new('0123456789abcdef', :ignore_case => true)
  base.to_i('F0A')
  => 3850

Anybase can also have synonyms.

  base = Anybase.new('0123456789abcdef', :synonyms => {'0' => 'oO'}, :ignore_case => true)
  base.to_i('FoA')
  => 3850
  base.to_i('FOA')
  => 3850

If you just want to translate the number to look at it you can use #normalize

  base = Anybase.new('0123456789abcdef', :synonyms => {'0' => 'oO'}, :ignore_case => true)
  base.normalize('FoA')
  => 'f0a'

If you want to use negative numbers, assign the sign character using the sign option

  base = Anybase.new('0123456789abcdef', :sign => '-')
  base.to_native(-12345)
  => '-3039'

Anybase can also zeropad your output with whatever your "zero" character is.

  Anybase.new("012345678").to_native(1234, :zero_pad => 8)
  => '00001621'

Anybase also lets you create random numbers of any number of digits

  Anybase.new("012345678").random(20)
  => '62400274212676317317'

Anybase gives you a few built-in:
<tt>Anybase::Hex</tt>, <tt>Anybase::Base64</tt>, <tt>Anybase::Base64ForURL</tt> and <tt>Anybase::Base62</tt>
(should all be pretty self-explanatory)

As well, there is an <tt>AnyBase::Base73ForURL</tt>. This includes the characters:
<tt>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$-_.+!*\'(),</tt>

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
anybase-0.0.15 README.rdoc
anybase-0.0.14 README.rdoc
anybase-0.0.13 README.rdoc
anybase-0.0.12 README.rdoc
anybase-0.0.11 README.rdoc