Hexadecimal system

The hexadecimal system, also known as base-16, is a number system that uses 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

This system is commonly used in computing because it allows for more compact representation of numbers. In the hexadecimal system, each digit represents a power of 16. For example, the number FF in hexadecimal can be represented as 15 * 16^1 + 15 * 16^0, or 255 in decimal.

Converting from decimal to hexadecimal

To convert a decimal number to hexadecimal, you can use the following steps:

  1. Divide the decimal number by 16.
  2. Take the remainder of the result from step 1 and convert it to the corresponding hexadecimal digit. If the remainder is 10 or greater, you can use the letters A, B, C, D, E, or F to represent the values 10 through 15, respectively.
  3. Divide the result from step 1 by 16 again, and repeat steps 2 and 3 until the result is 0.
  4. Write down the hexadecimal digits in the reverse order that you obtained them. The first digit you obtained will be the least significant digit, and the last digit you obtained will be the most significant digit.

For example, to convert the decimal number 255 to hexadecimal, you can do the following:

  1. Divide 255 by 16: 255 / 16 = 15, remainder 15
  2. Convert the remainder to a hexadecimal digit: 15 = F
  3. Divide the result from step 1 by 16 again: 15 / 16 = 0, remainder 15
  4. Convert the remainder to a hexadecimal digit: 15 = F
  5. Write down the hexadecimal digits in reverse order: F, F

Therefore, the hexadecimal representation of 255 is FF.

Converting from hexadecimal to decimal

To convert a hexadecimal number to decimal, you use the same algorithm in reverse:

  1. Write down the hexadecimal number, with the most significant digit on the left and the least significant digit on the right.
  2. For each digit in the hexadecimal number, multiply that digit by the corresponding power of 16. The first digit (the most significant digit) should be multiplied by 16^(n-1), where n is the total number of digits in the hexadecimal number. The second digit should be multiplied by 16^(n-2), and so on until the least significant digit, which should be multiplied by 16^0.
  3. Add all of the resulting values from step 2 together to get the decimal representation of the hexadecimal number.

For example, to convert the hexadecimal number FF to decimal, you can do the following:

  1. Write down the hexadecimal number: FF
  2. For each digit, multiply that digit by the corresponding power of 16: 15 * 16^1 + 15 * 16^0
  3. Add all of the values together: 15 * 16^1 + 15 * 16^0 = 255

Therefore, the decimal representation of FF is 255.

Printing hexadecimal numbers in JavaScript

In JavaScript it is really simple to print a number in binary format. Just specify the base on the .toString() method, like in the following example:

You can also consult the code in this tutorial about binary and hexadecimal numbers.

Other number systems

In addition to the hexadecimal number system, there are several other number systems that are commonly used in computing. One of these is the binary number system, which uses only two digits: 0 and 1. This system is used extensively in computing because it allows for the representation of data in a form that can be easily processed by a computer's circuitry.

Another common number system is the octal number system, which uses eight digits: 0, 1, 2, 3, 4, 5, 6, and 7. This system is used less frequently than binary and hexadecimal, but it can be useful in certain situations. For example, octal is often used to represent file permissions in the Unix operating system.

See also

See also the article about binary numbers.

Read more blog articles Browse JavaScript projects

About codeguppy

CodeGuppy is a FREE coding platform for schools and intependent learners. If you don't have yet an account with codeguppy.com, you can start by visiting the registration page and sign-up for a free account. Registered users can access tons of fun projects!


Follow @codeguppy on Twitter for coding tips and news about codeguppy platform. For more information, please feel free to contact us.