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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started