Question

In: Computer Science

1) Write a method that takes a string representing a positive binary number, and writes out...

1) Write a method that takes a string representing a positive binary number, and writes out the number in hex. (Extra credit opportunity. Write the method so that the input can be a binary number in two's compliment form.)

2) Write a method that takes an integer and writes out its value out in hex. You cannot use string formatting characters.

3) Write an encodeString method that encodes a string of ASCII Characters.

Input: It takes two parameters:

a) inputString: An string of visible ASCII characters.

b) key: A number must be between 1 and 8.

Output: A new string with the 8 bits of each character shifted left by the key and with random bits in the unused locations

4) Write a decodeString method that decodes a string of encoded characters

Input:

It takes two parameters:

a) inputString: An string of encoded characters.

b) key: A number must be between 1 and 8.

Output: A new string of ASCII character found by shifting right by the value of the key and with random bits cleared.

5) Write a breakCode method that attempts to decode an encoded string without knowing the key

Input: It takes one parameter. An encoded string

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

class Main
{
//Binary to Hexadecimal value
public static String binToHex(String binary)
{
int decimalValue = Integer.parseInt(binary,2);
String hexStr = Integer.toString(decimalValue,16);
return hexStr;
}
  
//Decimal to Hexadecimal value
static String decToHex(int decimalValue)
{
return Integer.toString(decimalValue,16);
}
  
//ENCODING
static String encodeString(String inputString, int key)
{
String encodedString="";
for(int i=0;i<inputString.length();i++)
{
int ch = (int) inputString.charAt(i);
//Left shift
encodedString += ""+(char) (ch<<key);
}
  
return encodedString;
}
  
//DECODING
static String decodeString(String inputString, int key)
{
String decodedString="";
for(int i=0;i<inputString.length();i++)
{
int ch = (int) inputString.charAt(i);
//Right Shift
decodedString += ""+(char)((ch>>key));
}
  
return decodedString;
}
  
//Break CODE
static void breakCode(String encodedString)
{
String possibleSolutions="";
for(int key=1;key<=8;key++)
{
possibleSolutions += decodeString(encodedString,key)+", ";
}
System.out.println("The Possible Solutions are: "+possibleSolutions);
}
  
//Test the methods
   public static void main (String[] args)
   {
   System.out.println("Binary 0110 to Hex: "+binToHex("0110"));
   System.out.println("Decimal 245 to Hex: "+decToHex(245));
  
   String str="012";
   String encodedString = encodeString(str,1);
   System.out.println(str+" is Encoded with key 1 to : "+encodedString);
  
   System.out.println(encodedString+" is decoded with key 1 to : "+decodeString(encodedString,1));

System.out.println("Performing CODE BREAK on "+encodedString);
  
breakCode(encodedString);
   }
}

===============

SCREENSHOT:

NOTE: we will get some no printable characters in the encoded strings...!!


Related Solutions

rogram that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
USE Coral Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is 6, the output is:011(6 in binary is 110; the algorithm outputs the bits in reverse).
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Java  Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
[C] Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
6.19 LAB: Convert to binary - functionsWrite a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The IntegerToReverseBinary function...
(Python) Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Python8.21 Program: Convert to binary. Sections 2.7, 3.8, 5.2. Functions.Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Your program must define and call the following two functions. The function integer_to_reverse_binary() should return...
Write a C++ program will read in the number of nodes and a binary relation representing...
Write a C++ program will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like this: It starts by...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
Write a program that inputs a string that represents a binary number. The string can contain...
Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console. Examples of...
Time String to Seconds Write a method that takes a string representation of time in hours:minutes:seconds...
Time String to Seconds Write a method that takes a string representation of time in hours:minutes:seconds and returns the total number of seconds For example, if the input is “0:02:20” it would return 140 If the input string is missing the hours component it should still work For example, input of “10:45” would return 645 Write an overloaded form of the method that takes in only hours and minutes and returns the total number of seconds It should leverage the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT