Last active
August 29, 2015 14:22
-
-
Save wmadden/f30178506367bad97d02 to your computer and use it in GitHub Desktop.
A way to convert a decimal number to an arbitrary base string representation and back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Converts a given decimal number into a base 26 string representation, e.g. | |
// 0 -> A | |
// 1 -> B | |
// 25 -> Z | |
// 26 -> BA | |
// 26*26 -> BAA | |
function stringCodeFromNumber(number) { | |
return stringifyNumberArray(convertDecimalToNewBase(number, 26, 0), "A"); | |
} | |
// Converts a string to a decimal number. Takes as arguments the base | |
// of the number represented in the string, and the zeroth letter. | |
// E.g. numberFromStringCode("BAA", 26, "A") -> 676 (26 * 26) | |
function numberFromStringCode(string, base, zerothLetter) { | |
var numberArray = numberArrayFromString(string, zerothLetter); | |
var result = 0; | |
for (var i = 0; i < numberArray.length; i++) { | |
var power = numberArray.length - i - 1; | |
result += numberArray[i] * Math.pow(base, power); | |
} | |
return result; | |
} | |
// Takes a decimal number and returns an array of numbers representing digits | |
// in the new base. | |
function convertDecimalToNewBase(number, newBase, power) { | |
var nextPower = power + 1; | |
var nextDivisor = Math.pow(newBase, nextPower); | |
var remainder = number % nextDivisor; | |
var divisor = Math.pow(newBase, power); | |
var digit = remainder / divisor; | |
var rest = number - remainder; | |
if (rest === 0) { | |
return [digit]; | |
} | |
return convertDecimalToNewBase(rest, newBase, power + 1).concat([digit]); | |
} | |
// Takes a number array and returns a string by converting each digit | |
// in the array into a letter based on the given zeroth letter. | |
function stringifyNumberArray(numberArray, zerothLetter) { | |
var result = ''; | |
var zerothLetterValue = zerothLetter.charCodeAt(0); | |
for(var i = 0; i < numberArray.length; i++) { | |
var number = numberArray[i]; | |
result += String.fromCharCode(zerothLetterValue + number); | |
} | |
return result; | |
} | |
// Converts a string into an array of digits, given the zeroth letter. | |
function numberArrayFromString(string, zerothLetter) { | |
var numberArray = []; | |
var zerothLetterValue = zerothLetter.charCodeAt(0); | |
for(var i = 0; i < string.length; i++) { | |
numberArray.push(string.charCodeAt(i) - zerothLetterValue); | |
} | |
return numberArray; | |
} |
It works by converting the given decimal number to a base 26 representation, then converting that to a string. Unfortunately, that doesn't really fit the scheme of A,B,C...Z, AA, AB, AC, ... ZZ, AAA because you can't use A to represent zero and one.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a lot more flexible than you need for your problem but it was bugging me.