Question: In this exercise, youll write a binary to text translator! As weve seen, every character can be represented by a string of 8 bits, or

In this exercise, youll write a binary to text translator!

As weve seen, every character can be represented by a string of 8 bits, or a byte. For example, the binary string 010000012 has the decimal value of 6510, which maps to the character A.

We can easily go from an int value between 0 and 255 to a char value using casting. For example, the following code:

int decimalValue = 65; char charValue = (char) decimalValue; System.out.println(decimalValue + " --> " + charValue);

Will print out

65 --> A

Using your binaryToDecimal method that you wrote earlier, write a method binaryToText that takes a String of bits, converts every 8 bits into the corresponding char, and returns a String representing the translated text.

For example:

binaryToText("0100100001001001");

Should return:

"HI"

To see why, lets examine this binary String:

"0100100001001001" Is made of 16 bits The first 8 bits are "01001000" binaryToDecimal("01001000") returns 72 (char) 72 equals 'H' The next 8 bits are "01001001" binaryToDecimal("01001001") returns 73 (char) 73 equals 'I' The resulting String is "HI"

You may assume that binaryToText will only be passed binary Strings that are made up of multiples of 8 bits.

public class BinaryConversion extends ConsoleProgram { public void run() { // Write some of your own test code here! } public String binaryToText(String binary) { // Write your code here! } public int binaryToDecimal(String binaryString) { // Copy over your binaryToDecimal method here // It will be useful when writing your binaryToText method }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!