1. Click here to get Solution on ProgAnswer.com
Converting a Number from Binary to Decimal, hexadecimal and octal:
Topics Covered: reading keyboard input, if statements, nested loops, and string manipulations
Your program will take as input a series of "binary digits" as a string and then will call methods to
convert and display the binary string converted into decimal, hexadecimal and octal.
Your main method is responsible for prompting the user for the binary number. It should then call a
method "isValid" to test if the string input consists of only zeros and ones.
If the string is valid your main program should call a method "printDecimal" that prints the value of the
binary number in decimal. It will then call methods "printHex" and "printOctal" to print out the
hexadecimal and octal representations of the string as well. If the string input was NOT a valid binary
number then the main program should print an error message.
Method: printDecimal
The specification for this method should be
public static void printDecimal(String binaryNum)
It is responsible for using loops to convert the binary number into decimal and printing out the result.
To convert a number from base 2 to base 10, we first find the weight of each bit in the binary number.
The weight of each bit in the binary number is assigned from right to left. The weight of the rightmost
bit is 0. The weight of the bit immediately to the left of the rightmost bit is 1, the weight of the next bit
to the left is 2 and so on. Consider the binary number 1001101. The weight of each bit is as follows:
Weight6 5 4 3 2 1 0
1 0 0 1 1 0 1
We use the weight of each bit to find the equivalent decimal number. For each bit, we multiply the bit
by 2 to the power of its weight and then add all of the numbers. For the above binary number, the
equivalent decimal number is:
= 1 * 26 + 0 * 25 + 0 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20
= 64 + 0 + 0 + 8 + 4 + 0 + 1
= 77
Remember: to convert a binary number to an equivalent decimal number:
1. The weight of each bit in the binary number must be determined.
2. The weight is assigned from right to left.
Method :printHex
The specification for this method is:
public static void printHex(String binaryNum)
To convert a number from binary into Hexadecimal you group together sets of 4 bits from right to left,
padding on the left with zeros if needed. (see lecture 1 for details). This method will need to:
If the length of the binary string is not evenly divisible by 4, then concatenate zeros on the left.
2. Take groups of 4 bits (4 characters from the string) and convert the 4 binary digits in decimal and print
out the correct hexadecimal digit remembering the values 10-15 are A-F
Method :printOctal
The specification for this method is:
public static void printOctal(String binaryNum)
To convert a number from binary into Octal you group together sets of 3 bits from right to left, padding
on the left with zeros if needed. (see lecture 1 for details). This method will need to:
If the length of the binary string is not evenly divisible by 3, then concatenate zeros on the left.
Take groups of 3 bits (3 characters from the string) and convert the 3 binary digits in decimal and print
out the correct decimal value of the 3 digits. NOTE only the values 0-7 should be printed for each group
of 3 bits.
Tasks:
1. NOTE: your main program MUST allow the user to input more than one binary string.
* You will begin by asking the user if they want to enter a number or quit. IF they select "quit" then no
further processing should happen and your program should quit. If they input a number your program
should do the following with each iteration:
a. Your program must prompt the user for a binary # as a sequence of 1's and 0's.
b. Your program will read the user's input as a String and then must VALIDATE that the string
contains ONLY 1's and 0's. If it contains any other character an error should be displayed and execution
should stop, and you should ask the user if they want to enter another number or quit.
c. If the string is valid, main should display the number input and then call printDecimal, printHex
and printOctal.
2. Your program should ask the user if they want to enter another binary number or quit.