RSS
 

Archive for January, 2014

Convert Base 10 to Base 64 and vise versa using JavaScript

13 Jan

This is the way to convert base 10 (a decimal number) into a base 64 string and base 64 into a base 10, while the order of that base is as following:

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-

Base 10 to Base 64

1
2
3
4
5
6
7
8
9
10
11
12
function base10_to_base64(num) {
    var order = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
    var base = order.length;
    var str = "", r;
    while (num) {
        r = num % base
        num -= r;
        num /= base;
        str = order.charAt(r) + str;
    }
    return str;
}

Read the rest of this entry »