markdown guide

It's used when converting between number systems.

I have some code with it here when I made a script to convert between ASCII, Hexidecimal, and Binary for me since I needed to do that often for a cryptography class.

To go from hex to decimal to ASCII, I did this:

case 'hex': {
        input.forEach(function(hex) {
          if (hex[0] == 0 && hex[1] == 'x') {
            hex = hex.slice(2);

          return String.fromCharCode(parseInt(hex, 16)) + ' ';

and to go from binary to decimal to ASCII, this:

      case 'binary': {
        input.forEach(function(binary) {
          return String.fromCharCode(parseInt(binary, 2)) + ' ';

Since hex numbers are in base 16, you need to tell parseInt to interpret a base 16 number. Or binary numbers are in a base 2 system, so radix is 2. If you don't provide a radix, it just defaults to 10 since that's what the "main" number system is.


There are different ways to represent the same number depending on which system you're using. You'll often here these systems referred to by the number of character in them. For instance, base 10 uses ten characters to represent numbers whereas base 2 (also called binary) uses two characters (0 and 1). Other common systems are base 8 (octal), base 16 (hexadecimal), and base 64. By way of example, the base 10 value 2 would be represented as 10 in base 2. Another word for "base" in this context is "radix". So radix is the number of characters used to represent values in a particular number system.

Alright, so what about parseInt? Well it converts strings to integers as you've probably already guessed. Usually people write numbers in base 10, so parseInt assumes you want to use base 10 if you don't specify something else. But sometimes you'll be calling parseInt on strings in one of these other systems such as binary or hexadecimal. In those cases, parseInt makes it easier to convert those values to integers by letting you specify whatever radix (aka base) you want.