Convert between Base 2 and Base 64/94
by skibulk
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/big-integer/1.6.48/BigInteger.min.js"></script>
JavaScript
console.clear();
/*
Problem: Some characters may be difficult to read / unknown by typical users (0O1I.,:;'"|`)
Solution:
Use font with distinctive characters
For dotted 0 | and ` give instructions on the data entry page.
Problem: IDs may contain inappropriate words.
Solution:
Dont use vowels?
Bonus - Don't need to check codes against dictionary
Solution:
Reserve one char (Z?) to break up words:
FUCK
FUZCK
If this would add multiple chars, try reversing the string and appending the reserved char:
FUCK
KCUFZ
*/
// BASE 62
// Note: Base 64 adds + and /
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var input = bigInt("99999", 62, alphabet).toString(2);
var output = bigInt(input, 2).toString(62, alphabet);
console.log(62, input.length, output.length, input, output);
// BASE 52
// Base 62 minus vowels
convert(45, 4);
// BASE 94
// Note: Spec excludes the space character
convert(94, 5);
// BASE 84
// Note: Base 94 minus vowels
convert(84, 5);
// BASE 182
// Base 94, minus _-=, plus text underline
convert(182, 5);
// BASE 93
// Base 94, minus Z
convert(95, 5);
function convert(base, strLen){
var alphabet = '';
for (let i = 0; i < base; i++) { alphabet += String.fromCharCode(33 + i) }
// Generate max str with strLen chars
var int = alphabet.substr(-1).repeat(strLen);
var intWrap = bigInt(int, base, alphabet);
// Convert string to MAX binary number
var binary = intWrap.toString(2);
var decimal = intWrap.toString(10);
var hex = intWrap.toString(16);
var custom = intWrap.toString(base, alphabet);
console.log(base, strLen, int.substr(0,64), alphabet);
console.log([
{ base: 2, length: binary.length, string: binary},
{ base: 10, length: decimal.length, string: decimal},
{ base: 16, length: hex.length, string: hex},
{ base: base, length: custom.length, string: custom},
]);
}
// ---------------------------
// 10KB of random data - TXT (Every printable keyboard character, space,...