When we learn to count, we use base 10, but computers count in base 2 (binary). The transistors in our machines manage only two states, on or off, corresponding to an electrical current. 1 corresponds to an on state, i.e., a non-zero current, while 0 corresponds to zero current.
To convert text to a binary representation, we first convert letters to their decimal representation. What we see on the keyboard has no existence for the computer. Each key corresponds to a number in decimal. For example, the letter "a" is coded as 97 by your computer. If we take the word "hello," we need to first convert it to decimal, which, according to the ASCII tables, would be:
104, 101, 108, 108, 111
Once this conversion is done, we convert each number into binary. For this, we take the number and apply a series of divisions by 2. For each division, we take the remainder (using the modulo operation), which will be a component of the binary representation. Then we apply this division by two until we reach 0. The remainders of the divisions are the binary representation, which we have to reverse to start from 0. For example, for the letter "a," which is 97:
97 % 2 = 1
48 % 2 = 0
24 % 2 = 0
12 % 2 = 0
6 % 2 = 0
3 % 2 = 1
1 % 2 = 1
Here we see that the binary representation of the letter "a" is 1100001 (we reverse it). Notice that here we use only 7 bits to represent "97". Indeed, the number 97 is not large enough to use the last bit. To check this, consider that each bit of a byte has an intrinsic value. Starting from the left, the first bit is worth 128, the second 64, then 32, 16, 8, 4, 2, 1. In our representation of the letter "a," we see that bits 64, 32, and 1 are activated. That is 64 + 32 + 1 = 97. On this page, we apply this very simple technique to convert text inputs to binary outputs. With binary to decimal conversion established, its conversion to octal is quite simple. We simply convert the decimal result to base eight as explained below. For binary to base64 conversion, we proceed first by converting to text, then to base64.
Hexadecimal (literally base 16) is a very convenient notation in computing as it is more compact than decimal for storing numbers (e.g., 2,000,000 in decimal is noted 1E8480 in hexadecimal). Moreover, one hexadecimal digit corresponds exactly to 4 binary bits, which allows representing one byte with 2 hexadecimal digits. This makes calculations easier, which is why hexadecimal is widely used in computing and quickly replaced octal notation (which can still be found in the Linux permissions system, with 777 being the maximum).
Binary to hexadecimal conversion is based on an extremely simple principle. We just need to split the byte into 2 parts of 4 bits, then assign each group of 4 bits its hexadecimal value (as mentioned, hexadecimal digits correspond to 4 bits). For example, our letter "a":
Binary: 0110 | 0001
Hexadecimal: 6 | 1
Here we take "0110," which is equal to 6 in decimal, thus 6 in hexadecimal, and "0001," which corresponds to 1 in hexadecimal. This gives us "61".
Octal base, as the name suggests, is a base 8 representation of numbers. It potentially dates back a long time. It was used in computing but was quickly superseded by hexadecimal representation, which has the same advantage of being a power of 2 but allows representing bytes always with 2 symbols. The conversion from binary to octal is quite simple. It involves grouping the bits in threes from the right. For example, with the following binary representation:
10110101
We "cut" this sequence into triplets from the left:
10 110 101
Then we calculate the decimal equivalent of each triplet, which we then concatenate:
2 6 5
Here, the octal conversion of the number 10110101 is 265.
The conversion from octal to decimal works as follows: first, we assign an index to each digit of an octal number, then we multiply each of these digits by 8 to the power of the corresponding index. Let's take the following example:
66427
We assign an index to each digit starting from the left:
6(4) 6(3) 4(2) 2(1) 7(0)
Then we multiply each digit by 8 to the power of its index and add each component:
(6 x 8^4) + (6 x 8^3) + (4 x 8^2) + (2 x 8^1) + (7 x 8^0)
Which gives us the following result:
24576 + 3072 + 256 + 16 + 7 = 27927
The decimal representation of the octal number 66427 is thus 27927.
For the conversion from octal to text, we first convert octal to decimal, then decimal to a textual representation using ASCII tables. For the conversion from octal to base64, we proceed as for text and then encode the text into base64.