Technical Article

How to Convert Numbers: Binary, Decimal, and Hex Systems

January 10, 2024 by David Peterson

Converting different numbering systems is quite common in PLCs and structured text codes. Why? Because people read decimals. Computers read binary. Processors read sets of bits. Not so simple.

I have always loved numbers, but in math classes, everything is based on the number 10 as the foundation for counting. Then you reach more advanced application classes, like computer programming, or even working with logarithms, and you learn that base 10 is not the only numbering system out there. There are three main systems that apply to engineers, and a fourth one that is still applicable in some cases. And friends, we are NOT going to talk about natural logarithms and Euler’s number here.

 

What is a Number Base?

The base of a number indicates the range of values a single number could possibly hold at any time. In base 10, each digit could be anywhere from 0 to 9, which makes 10 total options. In binary (base 2), you only get 2 options, either 0 or 1. Same for any other base number system.

 

Why Do Computers Use Binary?

Binary is the system of counting with only two options. The transistors that reflect memory storage can only be either ON or OFF, which leads to two options: base 2. When counting, we represent them as 1 and 0.

In the future, perhaps computers will use some other system, like maybe forward and reverse voltage, in addition to OFF. This might mean we switch to base 3, and computers can hold larger volumes of data? But I digress. As of now, no such system exists in mainstream implementation, so binary it remains.

 

Calculator with Bin to Dec to Hex conversion options

Figure 1. If you’re the calculator type, many scientific calculators have number conversion buttons, like my favorite, this one. The green buttons have a change-of-base function for all common systems. Image used courtesy of Casio

 

Why Do Programs Use Hexadecimal?

If computers do speak in binary terms, then why do so many programs use other bases, like 10 or 16?

Base 10 is used in computing for the sole purpose of making a value human-readable. If you have an analog temp sensor displaying the temperature in Fahrenheit, would you rather see: 10110011 degrees? Or maybe B3 degrees? Or perhaps 179 degrees? I agree… The last one is better for me.

The problem with base 10 is that computers store value in batches of 4 bits at a time (or 8, or all the way to 64 in most modern PCs). If we examine the 4-bit unit, it can hold anywhere from 0000 to 1111, which is equal to 15 (more on that calculation later). So there are 16 possible options, or base 16.

Why not use the more common 8-bit unit called the ‘byte’ as the number base? I suppose that is possible, but a byte value can be anywhere from 0 to 255, which is 256. This would lead to a base 256 number. The computer could handle it, but the poor engineers have a hard enough time translating hex numbers, please, let’s keep it reasonable.

 

Is Base 16 (Hex) The Same as a 16-bit Number?

NO. This is NOT the same. An unsigned 16-bit number can be anywhere from 0 to 65,536. To use a base 65536 system, we’d need to have a single digit that would count up to 65,535 before carrying over to the second decimal place.

Hex uses the characters A-F to represent the numbers beyond 9, because, let’s face it… 10 isn’t a number, it’s two numbers: a 1 and a 0. Imagine trying to choose a symbol to represent every single value from 10 all the way to 65,535. And then even worse… Trying to remember them….

For the sake of sanity, we limit the representation to base 16, made of 4 bits. Every byte will then be two hex characters. Every 16-bit integer will be 4 hex characters, and every 64-bit integer will be 16 hex characters. If you don’t like it? Well, tough luck for you, I guess.

 

Converting Binary <> Decimal

Converting between binary and decimal is one of the most common. Let’s break down the process in both directions.

 

Binary to Decimal Conversion

Each binary bit has a place value. Much like a series of light switches, the place values have either a 0 or a 1 associated with them. All we need to do is add up the place values that are ‘on’.

 Highest place value

   

Lowest place value 

8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit
128 64 32 16 8 4 2 1

In any 8-bit binary number, simply identify which bits are ‘on’ and add them up.

Example: What is the decimal equivalent of 10111001?

Looking at each place: 128+32+16+8+1 = 185.

If you need to convert a 16-bit number, just expand that table on the left by 8 more columns, and the 9th through 16th bits will keep doubling, each column one by one.

 

Decimal to Binary Conversion

The previous table is very helpful for the conversion of decimal numbers into binary.

We’ll start with a blank 8-bit placeholder: xxxxxxxx and fill in the values as we progress.

First, you must identify the largest bit value that you could extract out of a decimal number. Remember our temperature example from before? 179 degrees?

The largest binary place value we can extract from 179 would be 128. The next (9th) bit would be 256, which is bigger than 179.

So, we place a 1 in the 8th bit place:

 1xxxxxxx

 

Then we subtract 128 from the decimal value.

 179-128 = 51

 

Move on to the 7th bit place, which is 64. Can you subtract 64 from 51? No, so we place a 0 into the 7th bit place:

 10xxxxxx

 

Moving on to the 6th bit, which is 32. We can subtract 32 from 51, so we place a 1 into the 6th bit place:

 101xxxxx

 

And subtract 32 from 51:

 51-32 = 19

 

Moving to the 5th bit, 16. Yes. 16 can be subtracted from 19:

 1011xxxx

 

And then do the math:

 19-16 = 3

 

The 4th bit place is 8, and we cannot get 8 out of 3:

 10110xxx

 

Next is the 3rd bit, 4. Likewise, we cannot get 4 out of 3:

 101100xx

 

The 2nd bit is 2, and yes, now we can subtract 2 from 3:

 1011001x

 

The math is easy at this step:

 3-2 = 1

 

And now, finally, the 1st bit is 1. And indeed, we do have 1 left over, so the last bit is ON.

 10110011 is equal to 179.

 

Converting Hexadacimal <> Decimal

Here we enter into the world of A-F. No other numbering system has such a need of placeholder values to represent a single digit. Hex numbers are commonly preceded by 0x.

You just need to remember these: A=10, B=11, C=12, D=13, E=14, and F=15.

 

Hex to Decimal Conversion

We will build a similar table of place values, just like for the binary system. We’ll stick with only 4-digit hex numbers. You will most likely never need to know how to convert anything bigger than this by hand.

 Highest place value

Lowest place value 

4th place 3rd place 2nd place 1st place
4096 256 16 1

As you can see, the numbers increase MUCH faster than they did for binary!

This process is quite similar to the binary conversion, except the place values are no longer simply on/off. We must multiply the place value by the number within that place value.

Example: Convert 0x4A7C from hex to decimal.

Multiply each place value by the number in that place, then add. Remember what A-F are equal to - treat them like numbers when converting!

(4*4096)+(A*256)+(7*16)+(C*1)

– Remember that A=10 and C=12 –

16,384+2560+112+12 = 19,068

 

Decimal to Hex Conversion

This one is a little trickier than binary since it’s not just on/off.

Example: 7218

First, you must still identify the largest place value that be subtracted from the decimal number. In this case, the 4th place, 4096, is the largest place value we can get from our number. But we can only subtract 4096 one time, that’s it. So the 4th place gets a 1.

 1xxx

 

Subtract 4096 from 7218

 7218-4096 = 3122

 

Next, can we subtract the 3rd place, or 256 from this new value? Yes, but how many times? By my count, I can subtract 256 from 3122 a total of 12 times. And in Hex, 12=C.

 1Cxx

 

Next, our math must account for subtracting 256 those 12 times:

 3122-(256*12)=50

 

Once again, can I subtract 16 from 50? Yes, and how many times? 3 times.

 1C3x

 

Subtract 16*3:

 50-(16*3)=2

 

The final place value is 1. How many times can 1 be subtracted from 2? You guess it… 2.

 0x1C32 = 7218

 

Converting Hex <> Binary

This last process is the simplest when using a chart that matches each hex digit to its equivalent binary combination.

Binary Hex
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

 

Hex to Binary Conversion

Start with the hexadecimal number and replace each digit with its binary equivalent.

Example: Conver 0x3C1E from hex to binary.

Each character, one at a time, is replaced by its own 4-bit pattern. Sometimes, spaces or underscores are inserted in between each 4-bit section:

0011_1100_0001_1110

 

Binary to Hex Conversion

This conversion is also simple but requires one initial step.

First, add enough 0s to the left side of the number to create a perfect multiple of 4.

Example: Convert 11001011010 from binary to hex.

Looks like I have an 11-bit number. I need to add a 0 at the beginning to get 12 bits:

 011001011010

 

Make spaces or underscores if you need them. It will clarify the conversion immensely.

 0110_0101_101

 

Now, simply replace each 4-bit set with its hex equivalent from the chart:

 65A

 

So, 0x65A = 11001011010, and if you want to step back a few sections, it’s also 1626 in decimal.

 

Converting Numbers

Whew, this was a long article, at least for our normal published content. In reality, this subject is even more complex than this article might appear, but it should provide you with the basic groundwork for evaluating binary and hex numbers so that your solutions make sense… Even if you use a calculator for every number conversion in the future. I know I sure do, but it’s helpful to approach a solution with enough knowledge to judge if it’s valid or not.