Converting Binary To Octal number using HashMap in Java
Remember while we were converting octal to decimal we were taking 3 binary digits at a time. A similar approach will be used where here for every 3 digits we are having a corresponding number as in octal system we have numbers from 0 to ‘R-1’ where R represents base value of number system. As the name suggests, in an octal number system, R is equivalent to 8. Hence, the number is as follows: 0,1,2,3,4,5,6,7.
Now going by virtue of HashMaps in converting one thing is clear from here that that one way or other binary equivalent and octal equivalent are going to act as key-value pairs in our HashMap.
Octal Binary 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111
- Convert the binary number into a decimal number.
- Using HashMap we map every bit with its decimal values.
- When the bit is found it maps with the decimal number
- Prints and display the octal equivalent of the number.
Input 1 : 1011 Output 1 : 13 Input 2 : 1000 Output 2 : 10
Enter the binary number to be converted : 011 Octal equivalent : 3