Convert binary numbers to decimals

When handling cryptocurrency-related stuff, like private keys, seeds, etc... we often hear things like "256 bits", "128 bits",... but what does it mean?

Computers use their different type of memories to perform any kind of action. The smallest memory unit is called bit and it can assume only 0 and 1 as a value. More bits chained together are able to represent any kind of information that a computer may be able to read.

Indeed, computers are only able to manage numbers, even when we see text and pictures, computers are handling numbers. More in detail, computers manage binary numbers, only made by 0s and 1s, like 10001001.

In our tools, like Dice2Seed and DiceTracker we have seen that we could also paste a binary string to obtain mnemonic phrases or private keys. But, what are these binary strings?

How numbers work

We are used to counting with numbers like 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... These are base10 numbers but we could also have base16 numbers, base64 or also base2. The number expresses the number of available digits. With base10 we have 10 digits (from 0 to 9) and with base2 we only have 2 digits (0 and 1).

When counting, each time we reach the maximum value we add another digit on the left and start counting again. So when we are counting and we reach the number 9, we add a 1 on the left and start again from 0, obtaining the number 10; when we reach the number 99, we add a 1 on the left and obtain the number 100, and so on...

Each digit gets a value depending on its position in the number. The first digit is multiplied by 1 (which is the result of 10^0), the second one is multiplied by 10 (which is the result of 10^1), the second one is multiplied by 100 (which is the result of 10^2), and so on.

The same thing happens also with base2 numbers, but in this case, the first digit gets multiplied by 2^0, the second one gets multiplied by 2^1, the third one gets multiplied by 2^2, and so on.

How to convert binary to decimals

As seen above, every digit of a binary number assumes a certain value depending on its position. What we basically have to do is to multiply the digit's value (which may be 0 or 1) by 2 elevated to the digit's position. Let's consider the following example:

2^72^62^52^42^32^22^12^0
10001001

To convert the binary number 10001001 into a decimal number, we have to calculate this sum:

1*2^0 + 0*2^1 + 0*2^2 + 1*2^3 + 0*2^4 + 0*2^5 + 0*2^6 + 1*2^7

which is equal to

1*1 + 0 + 0 + 1*8 + 0 + 0 + 0 + 1*128 = 137

How to convert decimals to binary

We could also need to convert a base10 number into a base2 one. This may be a little bit more complex, but let's do that with a simple and practical approach.

We need to consider that in a binary number, every digit has a value that equals 2 times the previous one. So we'll have a sequence like 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...

We first need to find the biggest number of that sequence that is not greater than the one that we have to convert into a binary number.

So, let's hypothesize we want to convert 137 into a binary number:

  • is 64 greater than 137? NO, so it may be the first number we are looking for;
  • is 128 greater than 137? NO, so it may be the first number we are looking for instead of 64;
  • is 256 greater than 137? YES, so it is not a valid solution and 128 is the first number we are looking for.

128 is equal to 2^7 so we can put a 1 in the 8th position (starting from the right) of our binary number (do not forget that computers start counting from 0).

2^72^62^52^42^32^22^12^0
1???????

Now we need to find the second digit of our binary number. We can apply the same criteria to the remaining part of the number we are looking for.

The remaining part of the number we are looking for is equal to 137 - 128 = 9. So we need to find the bigger number in the sequence that is not greater than 9. It is very easy to find out that it is 8, which corresponds to the 4th position of our binary number (2^3). The middle ones will be 0s.

2^72^62^52^42^32^22^12^0
10001???

Now we have to find the next digit for the remaining part of the decimal number we are converting, which corresponds to 137 - 128 - 8 = 1. In this case we can easily find out that 1 is equal to the first number of our sequence, so we can place a 1 in the first position of our binary number (corresponding to 2^0).

2^72^62^52^42^32^22^12^0
10001001

So we obtained our binary number corresponding to 137, which is 10001001!

Final thoughts

In this post, we have seen a short example about how numbers work with computers. Of course, there is much more to know but, when handling cryptocurrencies, it could be useful to know what is intended for a binary number and which is its value. We also may never have to perform the above-seen operations by ourselves

Whit the same criteria, we could also encounter base16 numbers (called hexadecimal), base64 numbers and so on...

 

This article was updated on 26 Oct 2023