Created
October 28, 2014 05:57
-
-
Save Karlina-Bytes/aab6d5e123a06e9a646d to your computer and use it in GitHub Desktop.
JavaScript functions for converting between numerical bases two through sixteen.
This file contains hidden or 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 to and from any base in the set (2,16). | |
* Assumes that the digitString and inputBase match. | |
* @param {String} digitString is a sequence of digits. | |
* @param {Number} inputBase is in the set (2,16) | |
* @param {Number} outputBase is in the set (2,16) | |
* @return {String} result is a string of digits 2-F | |
********************************************************/ | |
function baseConvert( digitString, inputBase, outputBase ) { | |
// First, convert the inputNumber to its decimal form. | |
var decimalValue = 0; | |
if (inputBase == 10) decimalValue = parseInt(digitString, 10); | |
else decimalValue = convertToDecimal(digitString, inputBase); | |
// Next, convert the decimalValue to its outputBase form. | |
if (outputBase == 10) return decimalValue; | |
return convertToBase(decimalValue, outputBase); | |
} | |
/********************************************************* | |
* Converts from any base in the set (2,16) to decimal. | |
* Assumes that the inputNumber and inputBase match. | |
* @param {String} inputDigits is a string of digits | |
* @param {Number} inputBase is in the set (2,16) | |
* @return {Number} a decimal value | |
********************************************************/ | |
function convertToDecimal( inputDigits, inputBase ) { | |
// decimalResult is a sum of products in base-ten. | |
var decimalResult = 0; | |
// placeValue represents the value of a digit position. | |
// The rightmost position is the one's place (any_base ^ 0). | |
var placeValue = 1; | |
/* STEP 1: Extract the rightmost digit from the string. | |
* STEP 2: Multiply the digit by its position value. | |
* STEP 3: Add that product to the decimalResult. | |
* STEP 4: Remove the rightmost digit from the string. | |
* STEP 4: Repeat for all digits. | |
*/ | |
while ( inputDigits.length > 0 ) { | |
// Extract the rightmost character from inputDigits. | |
var digitChar = inputDigits[ inputDigits.length-1 ]; | |
// Convert this character to its value in base-ten. | |
var digitNum = parseInt( ("" + digitChar), inputBase ); | |
// Find the value of the digit based on its position. | |
var digitValue = digitNum * placeValue; | |
// Add this product to the result sum. | |
decimalResult += digitValue; | |
// Increment the base place exponentially. | |
placeValue *= inputBase; | |
// Shorten the digit string by removing the rightmost char. | |
inputDigits = inputDigits.slice(0, (inputDigits.length-1) ); | |
} | |
// Return the sum of digit values in base-ten. | |
return decimalResult; | |
} | |
/********************************************************* | |
* Converts from decimal to any base in the set (2,16). | |
* @param {Number} decimalInput is a base-ten number | |
* @param {Number} outputBase is in the set (2,16) | |
* @return {String} digitString in the outputBase | |
********************************************************/ | |
function convertToBase( decimalInput, outputBase ) { | |
// letterDigits represent digits 10 through 16. | |
var letterDigits = ['A','B','C','D','E','F']; | |
// digitString is a number in the outputBase to return. | |
var digitString = ""; | |
// placeValue is a power of ten | |
var placeValue = 1; | |
/* STEP 1: Divide the decimalInput by the outputBase. | |
* STEP 2: Store the remainder of the division as digit. | |
* STEP 3: Append this digit to the front of digitString. | |
* STEP 4: Repeat for all digits. | |
*/ | |
while ( decimalInput > 0 ) { | |
// Extract the rightmost digit from decimalInput. | |
var digit = decimalInput % outputBase; | |
// If the digit is higher than 9, | |
// convert it to the appropriate character. | |
if (digit > 9) { | |
for (var i = 10; i <= 16; i++) { | |
if (digit == i) { | |
// Append the letter digit to digitString. | |
digitString = letterDigits[i-10] + digitString; | |
break; | |
} | |
} | |
} | |
// Otherwise, append the digit to digitString as is. | |
else digitString = digit + digitString; | |
// Remove te rightmost digit from decimalInput. | |
decimalInput = Math.floor(decimalInput / outputBase); | |
} | |
// Return the sum of digit values. | |
return digitString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment