Steven Olmsted![]()
Y.AnyBaseConverter is an object that will convert numbers to and from a positional notation with a custom alphabet and base.
Numbers are generally displayed in base ten. This means there are ten single-digit numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) before the first two-digit number, 10. Sometimes it is useful to work with numbers in other bases. Base 2, base 8, and base 16 tend to come up frequently in programming. JavaScript provides a toString method on numbers which can convert a number to any base from base 2 to base 36 and a parseInt function for converting the strings back into numbers. For bases greater than 10, lower-case letters are used for single digits greater than 9. For example, base 16 uses these single-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f.
Y.AnyBaseConverter is very similar to number.toString and parseInt except it can convert to and from bases greater than 36 and use a custom alphabet to represent the single-digit numbers. In addition to any convenient mathematical properties, it may be helpful to convert to a higher base as a simple way to compress a large number as a string or to use a custom alphabet as a method of obfuscation.
More information about positional notation and bases:
http://en.wikipedia.org/wiki/Positional_notation
http://en.wikipedia.org/wiki/Radix
Y.AnyBaseConverter has three attributes:
alphabet: The string of characters to use as single-digit numbers. The length of this string determines the base of the result. Each character should be unique within the string or else it will be impossible to correctly convert a string back into a number.
minusSign: A single character string to prepend to negative values. This character should not be in the alphabet.
radixPoint: A single character string to insert between the integer and fractional parts of the number. This character should not be in the alphabet.
Y.AnyBaseConverter has two methods:
from(string): converts a string from a custom base and returns a number.
to(number): converts a number to a custom base and returns a string.
Y.AnyBaseConverter currently does not support non-BMP characters.
With a proper alphabet, Y.AnyBaseConverter can produce the same results as number.toString(n) and parseInt(string, n) when working with integer values. Y.AnyBaseConverter handles the fractional part of a number differently.
<script src="http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js"></script>YUI({
//Last Gallery Build of this module
gallery: 'gallery-2013.05.02-22-59'
}).use('gallery-any-base-converter', function(Y) {
var converter;
// By default, Y.AnyBaseConverter comes with a base 64 alphabet which supports
// floating-point numbers, negative numbers, and is completely URL safe.
// (This is not quite the same thing as base 64 encoding.)
converter = new Y.AnyBaseConverter();
// convert a number to a string
converter.to(1234567);
// returns '4iQ7'
// convert a string to a number
converter.from('4iQ7');
// returns 1234567
// Here are some other alphabets to try:
//
// Binary - Used primarily by computer hardware.
//
converter = new Y.AnyBaseConverter({
alphabet: '01'
});
converter.to(1234567);
// returns '100101101011010000111'
//
// Base 58 - Only uses alphanumeric digits that are easily human readable.
//
converter = new Y.AnyBaseConverter({
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
});
converter.to(1234567);
// returns '7Kze'
//
// Confusing - Same base, same characters, different order.
//
converter = new Y.AnyBaseConverter({
alphabet: '8641359207'
});
converter.to(1234567);
// returns '6413592'
});
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info