Beetwise

Convert decimal to hex

To convert a decimal value into hex, divide the value by 16 and keep the remainder.

Repeat the division until the quotient is 0.

Read the remainders backwards, and write each one as a hex digit.

Try it

The value 0x12C in a 16-bit register. Select a bit to change it. Change the bit width or the signed mode to see the effect.

Name or expression
ID0
Binary
0b
Hex
0x
Decimal
ASCII
·,
Shift
Sign
Bits
Endian
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0

Worked example

Convert 300 into hex:

  1. 300 divided by 16 is 18, remainder 12. The digit for 12 is C.
  2. 18 divided by 16 is 1, remainder 2. The digit is 2.
  3. 1 divided by 16 is 0, remainder 1. The digit is 1.
  4. Read the digits backwards: 0x12C.

Where this is used

A data sheet gives a register address in hex, so a hex form is easier to compare.

A hex value also shows the bit groups, because one digit covers four bits.

Points to note

  • The digits after 9 are A, B, C, D, E and F. The digit F is 15.
  • Hex is not case sensitive. The values 0x12c and 0x12C are the same.
  • Three hex digits are not a whole number of bytes. Write 0x012C for a two-byte value.

The full tool

The full editor holds many values at the same time. It also evaluates expressions across them, and it reads live values from a serial port.

Open the full editor

Questions

What is 300 in hex?
The value 300 is 0x12C. As a two-byte value it is 0x012C.
What does the prefix 0x mean?
The prefix 0x marks a hex value. Without the prefix, 12C is not a valid decimal value.
How many bits is one hex digit?
One hex digit holds four bits, which gives the values 0 to 15.