In: Computer Science
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
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...!!