In this article, we will learn how to convert from decimal to binary number format and from binary to decimal format.
Decimal Number: The digits from 0 to 9 are called decimal numbers. It is also referred to as Base 10 because there are 10 digits in the decimal number system.
Ex : (29) 10
Binary Number: The digits 0 and 1 are called binary numbers. It is also referred to as Base 2 as there are only 2 digits in the binary number system. Each digit is treated as a bit.
Converting decimal to binary
1 ) Divide the decimal number by 2, we will get a quotient and a remainder. We will store the remainder in an array.
2 ) We will take the quotient and divide it by 2, again we will get a quotient and a remainder. We will store the remainder in an array again.
3 ) We have to repeat Step 2 till the quotient is 0.
4 ) Now we will accumulate the remainders from end to start of the array and hence we got our binary number.
Converting binary to decimal
1 ) The position of the last digit in a binary number is 0, so we will multiply the last digit by 20. Consider ( 101 ) as a binary number its last digit is 1, so we will multiply 1 by 20.
2 ) The position of the second last digit is 1, so we will multiply the second last digit by 21.
3 ) The position of the third last digit is 2, so we will multiply the third last digit by 22.
and so on till the very first digit . . . . .
(11101) 2 = 16 + 8 + 4 + 0 + 1
(11101) 2 == (29) 10
Hope you got an understanding of how we convert from decimal to binary number format and vice versa. We will use this conversion technique in Base64 encoding.