Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6102227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:35:25+00:00 2026-05-23T13:35:25+00:00

In JavaScript you can convert a number to a string representation with a specific

  • 0

In JavaScript you can convert a number to a string representation with a specific radix as follows:

(12345).toString(36) // "9ix"

…and you can convert it back to a regular number like this:

parseInt("9ix", 36) // 12345

36 is the highest radix you can specify. It apparently uses the characters 0-9 and a-z for the digits (36 total).

My question: what’s the fastest way to convert a number to a base 64 representation (for example, using A-Z, and - and _ for the extra 28 digits)?


Update: Four people have posted responses saying this question is duplicated, or that I’m looking for Base64. I’m not.

“Base64” is a way of encoding binary data in a simple ASCII character set, to make it safe for transfer over networks etc. (so that text-only systems won’t garble the binary).

That’s not what I’m asking about. I’m asking about converting numbers to a radix 64 string representation. (JavaScript’s toString(radix) does this automatically for any radix up to 36; I need a custom function to get radix 64.)


Update 2: Here are some input & output examples…

0   → "0"
1   → "1"
9   → "9"
10  → "a"
35  → "z"
61  → "Z"
62  → "-"
63  → "_"
64  → "10"
65  → "11"
128 → "20"
etc.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:35:26+00:00Added an answer on May 23, 2026 at 1:35 pm

    Here is a sketch for a solution for NUMBERS (not arrays of bytes 🙂

    only for positive numbers, ignores fractional parts, and not really tested — just a sketch!

    Base64 = {
    
        _Rixits :
    //   0       8       16      24      32      40      48      56     63
    //   v       v       v       v       v       v       v       v      v
        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/",
        // You have the freedom, here, to choose the glyphs you want for 
        // representing your base-64 numbers. The ASCII encoding guys usually
        // choose a set of glyphs beginning with ABCD..., but, looking at
        // your update #2, I deduce that you want glyphs beginning with 
        // 0123..., which is a fine choice and aligns the first ten numbers
        // in base 64 with the first ten numbers in decimal.
    
        // This cannot handle negative numbers and only works on the 
        //     integer part, discarding the fractional part.
        // Doing better means deciding on whether you're just representing
        // the subset of javascript numbers of twos-complement 32-bit integers 
        // or going with base-64 representations for the bit pattern of the
        // underlying IEEE floating-point number, or representing the mantissae
        // and exponents separately, or some other possibility. For now, bail
        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 = '';
            while (true) {
                rixit = residual % 64
                // console.log("rixit : " + rixit);
                // console.log("result before : " + result);
                result = this._Rixits.charAt(rixit) + result;
                // console.log("result after : " + result);
                // console.log("residual before : " + residual);
                residual = Math.floor(residual / 64);
                // console.log("residual after : " + residual);
    
                if (residual == 0)
                    break;
                }
            return result;
        },
    
        toNumber : function(rixits) {
            var result = 0;
            // console.log("rixits : " + rixits);
            // console.log("rixits.split('') : " + rixits.split(''));
            rixits = rixits.split('');
            for (var e = 0; e < rixits.length; e++) {
                // console.log("_Rixits.indexOf(" + rixits[e] + ") : " + 
                    // this._Rixits.indexOf(rixits[e]));
                // console.log("result before : " + result);
                result = (result * 64) + this._Rixits.indexOf(rixits[e]);
                // console.log("result after : " + result);
            }
            return result;
        }
    }
    

    UPDATE: Here’s some (very lightweight) testing of the above, for running in NodeJs where you have console.log.

    function testBase64(x) {
        console.log("My number is " + x);
        var g = Base64.fromNumber(x);
        console.log("My base-64 representation is " + g);
        var h = Base64.toNumber(g);
        console.log("Returning from base-64, I get " + h);
        if (h !== Math.floor(x))
            throw "TEST FAILED";
    }
    
    testBase64(0);
    try {
        testBase64(-1);
        }
    catch (err) {
        console.log("caught >>>>>>  " + err);
        }
    try {
        testBase64(undefined);
        }
    catch (err) {
        console.log("caught >>>>>>  " + err);
        }
    try {
        testBase64(null);
        }
    catch (err) {
        console.log("caught >>>>>>  " + err);
        }
    try {
        testBase64(Number.NaN);
        }
    catch (err) {
        console.log("caught >>>>>>  " + err);
        }
    try {
        testBase64(Number.POSITIVE_INFINITY);
        }
    catch (err) {
        console.log("caught >>>>>>  " + err);
        }
    try {
        testBase64(Number.NEGATIVE_INFINITY);
        }
    catch (err) {
        console.log("caught >>>>>>  " + err);
        }
    
    for(i=0; i<100; i++)
        testBase64(Math.random()*1e14);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert a JavaScript string value to be in all lowercase letters?
How can I convert a string to a date time object in javascript by
How can we convert a JavaScript string variable to decimal? Is there a function
I have a JSON string that I would like to convert to a javascript
How can I convert a character to its ASCII code using JavaScript? For example:
In javascript you can easily create objects and Arrays like so: var aObject =
Like the Delicious submission bookmark-let, I'd like to have some standard JavaScript I can
Is there a javascript function I can use to detect whether a specific silverlight
It doesn't look like basic javascript but nor can I use JQuery commands like
Is there a tool to convert javascript to java, so I can handle the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.