Skip to content Skip to sidebar Skip to footer

How To Change An Integer Into A String With Specified Character Set In Javascript

Given an integer input var i = 1234567890 based on the output of a hash function, I need to create a shortened, case sensitive alphanumeric string. This is for purposes of having

Solution 1:

This is a variant of a "Base64" conversion question that can be answered by "base n" libraries. However, these libraries may be "overkill" for this question, so below is modified code based on a simple & elegant solution by @Reb.Cabin. Credit also to editors @callum, @Philip Kaplan, @Oka on this code.

In this response, vowels and various "problem letters" commonly used to create curse words are removed, so a random integer hash will not create an offensive short URL.

// Based on Base64 code by @Reb.Cabin, edits by @callum, @philip Kaplan, @Oka available at https://stackoverflow.com/a/6573119/3232832BaseN = {
    _Rixits :
//   0       8       16      24      32      40      48      56     63//   v       v       v       v       v       v       v       v      v"0123456789BDGHJKLMNPQRTVWXYZbdghjklmnpqrtvwxyz-_",
//  original base64//  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_",// You have the freedom, here, to choose the glyphs you want for // representing your base-64 numbers.// This cannot handle negative numbers and only works on the //     integer part, discarding the fractional part.
    fromNumber : function(number) {
        if (isNaN(Number(number)) || number === null ||
            number === Number.POSITIVE_INFINITY)
            throw"The input is not valid";
        if (number < 0)
            throw"Can't represent negative numbers now";

        var rixit; // like 'digit', only in some non-decimal radix var residual = Math.floor(number);
        var result = '';
        var rixitLen = this._Rixits.length;
        while (true) {
            rixit = residual % rixitLen;
            result = this._Rixits.charAt(rixit) + result;
            residual = Math.floor(residual / rixitLen);

            if (residual === 0)
                break;
            }
        return result;
    },

    toNumber : function(rixits) {
        var result = 0;
        for (var e = 0; e < rixits.length; e++) {
            result = (result * this._Rixits.length) + this._Rixits.indexOf(rixits[e]);
        }
        return result;
    }
};

var i = 1234567890;
var encoded = BaseN.fromNumber(1234567890);
var decoded = BaseN.toNumber(encoded);
document.writeln('Given character set "' + BaseN._Rixits + '", the number ' + i + ' is encoded to ' + encoded + ' then back again to ' + decoded + '.');

Post a Comment for "How To Change An Integer Into A String With Specified Character Set In Javascript"