Convert binary and hexadecimal number - Here's how
Related Videos: Converting from decimal to hexadecimal representation | Pre-Algebra | Khan Academy (May 2024).
When programming or doing math, you've probably come across binary and hexadecimal numbers. This practical tip shows you how to convert them correctly.
Convert binary number into the tens system - how it works
Computers usually calculate with binary numbers or a dual system. So there are only two numbers: 0 and 1. These represent computers for "on" and "off".
- Let's take the number "101010" as a first example, which you would like to convert into the normal decimal system ("decimal system").
- To do this, start from the right: There is a 0 on the far right, so make a note of "0 ⋅ 2⁰".
- Next, take the number one digit to the left and add the whole thing to your result: "0 ⋅ 2⁰ + 1 ⋅ 2¹". The further a number is from the rightmost number, the greater the potency.
- Now repeat these steps for all numbers. As a result you should now get "0 ⋅ 2⁰ + 1 ⋅ 2¹ + 0 ⋅ 2² + 1 ⋅ 2³ + 0 ⋅ 2⁴ + 1 ⋅ 2⁵".
- You can then convert the powers into normal integers: "0 ⋅ 1 + 1 ⋅ 2 + 0 ⋅ 4 + 1 ⋅ 8 + 0 ⋅ 16 + 1 ⋅ 32".
- The number "101010" in the dual system in the tens system is the number "42".
- Tip: If this method of calculation is too difficult for you, you can also memorize the table that you see in the picture above.
Convert decimal number to binary number
Converting a tens to a binary number is even easier than converting a binary number to a decimal number.
- In this example we use the number "42" again.
- Divide this number by 2: "42: 2 = 21 remainder 0".
- Then divide the result of the previous calculation by 2: "21: 2 = 10 remainder 1".
- Repeat these steps several times until you get the calculation "0: 2 = 0 rest 0". The same result would always come from here; So you can stop the bill.
- Your calculation should now look like this: "42: 2 = 21 remainder 0; 21: 2 = 10 remainder 1; 10: 2 = 5 remainder 0; 5: 2 = 2 remainder 1; 2: 2 = 1 remainder 0 ; 1: 2 = 0 remainder 1; 0: 2 = 0 remainder 0; ...
- Now always write down the rest of each invoice. However, start from the back. You should now get the number "0101010".
- After all, you just have to leave out all the zeros up to the first 1. The number "42" is therefore the number "101010" in the dual system.
Convert decimal number to hexadecimal system - how it works
Converting a number into the hexadecimal system is a bit more complicated.
- As an example, we use the number "2017" this time.
- Divide this number by 16 and note the rest: "2017: 16 = 126 rest 1".
- Now you have to divide the result of the previous calculation by 16 again: "126: 16 = 7 rest 14".
- Repeat the steps until you have reached the calculation "0: 16 = 0 rest 0".
- Your calculation should now look like this: "2017: 16 = 126 remainder 1; 126: 16 = 7 remainder 14; 7: 16 = 0 remainder 7; 0: 16 = 0 remainder 0; ...
- Here too, just like when converting to a dual system, you have to write down the rest of each invoice one after the other. However, there are 16 numbers in the hexadecimal system. The numbers 0 to 9 remain the same. However, if a remainder is larger than 9, you must convert it to a letter. The following applies: "10 = A; 11 = B; 12 = C; 13 = D; 14 = E; 15 = F".
- If you note the remainder, you should get the number "07E1". Again, you can leave out the zeros at the beginning. The number "2017" is the number "7E1" in the hexadecimal system.
- Tip: So that you can calculate the remnants faster, it is sufficient to multiply the numbers of a quotient after the decimal point by 16: "126: 7 = 7.875 → 126: 7 = 7 remainder (16 ⋅ 0.875) → 126: 7 = 7 Rest 14 ".
Convert hexadecimal number to normal decimal number
Converting a hexadecimal number to a normal decimal number works similarly to converting a binary number.
- As an example we use the hexadecimal number "MONKEY". As you already know, the "A" stands for a 10, the "F" for a 15 and the "E" for a 14.
- Start calculating on the far right and write down "14 ⋅ 16⁰".
- Now go one place to the left and add the whole thing to your result: "14 ⋅ 16⁰ + 15 ⋅ 16¹". As you can see, the calculation works similarly to converting a binary number.
- In the end, your invoice should look like this: "14 ⋅ 16⁰ + 15 ⋅ 16¹ + 15 ⋅ 16² + 10 ⋅ 16³". The result is "45054".
Hexadecimal in binary - and vice versa
In the next paragraph, we would like to finally show you how you can convert a hexadecimal number into a binary number - and vice versa.
- As you may know, 16 different numbers with exactly 4 digits can be represented in the dual system, since 2⁴ = 16.
- Divide the binary number of your choice into four-packs: "1010 1111 1111 1110"
- You can then convert each pack of four into a decimal number to make it easier to assign the appropriate hexadecimal number.
- Conversely, you can also convert each digit of a hexadecimal number individually into a dual number.
0x and 0b - for what the whole thing?
You have probably already noticed that some hexadecimal or binary numbers have "0x" or "0b" in front of them.
- The "0x" is sometimes prefixed with a hexadecimal number so that it is also recognized as a hexadecimal number.
- For example, "0b" is often written before binary numbers.
- The "x" in "0x" stands for the "x" in "hexadecimal", the "b" in "0b" for "binary number".
- To make it easier to tell the numbers apart, brackets are placed around them (especially in mathematics): "(MONKEY) ₁₆". The 16 in the index stands for the hexadecimal system. Numbers in the dual system are therefore indicated with "(101010) ₂".
In the next practical tip, you will learn how to create and use arrays with the "Python" programming language.
$config[ads_text6] not found