Languages
[Edit]
EN

JavaScript - convert bin, oct, hex, etc. to decimal number (from any base system)

5 points
Created by:
Zoya-Gaines
683

In this short article, we would like to show how convert number values in any base system to the decimal number using JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

var hex = '0x7b';  // hex number
var base = 16;     // from base 16 number (from hexadecinal number)

var dec = parseInt(hex, base);

console.log(dec);  // 123

 

Custom implementation

The below function is able to convert to decimal from:

  • bin (binary system),
  • oct (octal systems),
  • hex (hexal / hexadecimal system),
  • any indicated but not greater than 36 (add more characters to the alphabet to get greater base systems).

 

1. Only positive input integers 

// ONLINE-RUNNER:browser;

const LIMIT = 37;  // [ALPHABET size] + 1
const ALPHABET = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35};

function convertBase(text, base) {  // add to ALPHABET more characters to get greater base
	if (text == null || text === '') {  // null or undefined or empty
      	return NaN;
    }
  	if (base > 1 && base < LIMIT) {
        let result = 0;
        for (const entry of text) {  // we iterate over symbols
            const index = ALPHABET[entry];
            if (index == null) {  // null or undefined
                throw new Error('Incorrect input value.');
            }
            result = result * base + index;
        }
        return result;
    } else {
        throw new Error(`Base value should be in range from 2 to ${LIMIT}.`);
    }
}



// Usage example:

//                    value  base
console.log(convertBase('', 2));         // NaN (from base 2)   <--- from bin
console.log(convertBase('1111011', 2));  // 123 (from base 2)   <--- from bin
console.log(convertBase('11120', 3));    // 123 (from base 3)
console.log(convertBase('1323', 4));     // 123 (from base 4)
console.log(convertBase('443', 5));      // 123 (from base 5)
console.log(convertBase('323', 6));      // 123 (from base 6)
console.log(convertBase('234', 7));      // 123 (from base 7)
console.log(convertBase('173', 8));      // 123 (from base 8)   <--- from oct
console.log(convertBase('123', 10));     // 123 (from base 10)  <--- from dec / identity
console.log(convertBase('7B', 16));      // 123 (from base 16)  <--- from hex
console.log(convertBase('3R', 32));      // 123 (from base 32)
console.log(convertBase('3F', 36));      // 123 (from base 36)
console.log(convertBase('10', 36));      // 36 (from base 36)

 

See also

  1. JavaScript - convert decimal number to bin, oct, hex, etc. (to any base system)

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join