• Register
  • Log In
  • Home
  • Quick Start
    • Configurator
    • Download YUI 3
  • Documentation
    • User Guides
    • Examples
    • API Docs
    • Environments
    • Tutorials
  • Community
    • Gallery
    • Blog
    • Forums
    • YUI Theater
    • Calendar
  • Contribute
    • YUI on GitHub »
    • File a Ticket
    • View Tickets
    • Dashboard
  • Other Projects
    • Shifter »
    • Yogi »
    • YUI 2
    • YUI Doc »
    • YUI Test
    • YUI Website
    • YUI Compressor »
    • YUI Builder »
    • YUI PHP Loader
    • Grid Builder »
    • Skin Builder »
  • YUI
  • >
  • Community
  • >
  • Gallery

Gallery

Modules

  • Home
  • Featured
  • Popular
  • New
  • All

Documentation

  • Yogi Documentation
  • Shifter Documentation
  • Developer Guide
  • Module Setup

Tag Cloud

Context Navigation

    YUI Library is not responsible for bugs or support with this module. It is available as a free service. For support please contact the module owner with the provided links.

    Any Base Converter (gallery-any-base-converter) on cdn

    Last Updated: 06/21/12
    + 0 -

    Steven Olmsted

    YUI Contributor

    See 42 more by this user.

    Created: 02/22/12
    Last CDN Push: 05/2/13
    Build Tag: gallery-2013.05.02-22-59
    Project: YUI 3
    License: YUI BSD
    YUI Version: 3.5.1
    Free for use.

    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

    • Tags:
    • alphabet
    • convert
    • converter
    • number
    • radix
    • solmsted
    • base
    • string
    • Download
    • Docs
    • Homepage
    • Bugs
    • Source
    • Example
    • Forum
    • History

    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.

    Code Sample

    <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