I had posted a question yesterday about this program I have been working on but essentially I've programmed an encoder of sorts that takes a string and an integer and then adds to every character within the string the value of the integer and then finally prints the binary of the string assuming 8 bits for every symbol or character. For example ABC with integer(code) 4 should output 01000101 01000110 01000111 (EFG). Now, I've managed to get super close to completing this project but I've run into an issue when converting each character to binary which I assume is due to how I'm actually calculating the binary. Example: If a binary integer starts with a 0, then obviously I will need to pad the integer with a 0 on the far left. I've managed to do that but there are cases where the integer ends in 0 and I need to do the same thing except on the far right... I'm not sure if I'm making 100% sense but I have posted my code and test result with expected outcome below. As you can see, it converts almost perfectly except the value in the middle (B) adds an extra 2 zeroes instead of just one on the far left and I haven't figured out how to check to add an ending zero. Can someone please help me? How I should go ahead and handle converting to binary(8 bits) from characters?
I also need to figure out how to decode binary into the original message. Example: InputMessage: 01000101 01000110 01000111, InputCode: 4 OriginalMessage: ABC
public class Encoder{
public static void main (String[] args) {
String msg;
int code;
int i;
msg = getMsg();
code = getCode();
getBinaryMsg(getCodedMsg(msg, code));
}
public static String getMsg(){
String msg;
System.out.print("Input message: ");
Scanner input = new Scanner(System.in);
msg = input.nextLine();
return msg;
}
public static int getCode(){
int code=0;
System.out.print("Input Code from 1 - 10: ");
Scanner input = new Scanner(System.in);
return input.nextInt();
}
public static String getCodedMsg(String msg, int code){
int letterOrDigit;
String codedMessage = "";
for(int i = 0; i<= msg.length()-1; i++){
letterOrDigit = msg.charAt(i);
if(Character.isLetter(letterOrDigit)){
letterOrDigit = (char)(msg.charAt(i)+code);
}
if((Character.isLowerCase(msg.charAt(i)) && letterOrDigit > 'z') || (Character.isUpperCase(msg.charAt(i)) && letterOrDigit > 'Z')){
letterOrDigit = (char)(msg.charAt(i) - (26 - code));
}
if(Character.isDigit(letterOrDigit)){
letterOrDigit = (char)(msg.charAt(i)+code);
}
if(Character.isDigit(msg.charAt(i)) && letterOrDigit > '9'){
letterOrDigit = (char)(msg.charAt(i) - (10 - code));
}
codedMessage +=(char)letterOrDigit;
}
return codedMessage;
}
public static void getBinaryMsg(String codedMessage){
char[] strChar = codedMessage.toCharArray();
int character;
int remainder;
int binaryInt;
int revBinInt;
int firstDigit;
String paddedWithZero = "";
String binaryMsg = "";
for(int i = 0; i < strChar.length; i++){
binaryInt = 0;
revBinInt = 0;
firstDigit = 0;
character = strChar[i];
//Calculating 8 binary bits
for(int j = 0; j <= 7; j++){
remainder = character % 2;
binaryInt = binaryInt * 10 + remainder;
character = character / 2;
}
//Reversing the above for loop so that binary is correct
while(binaryInt != 0){
remainder = binaryInt % 10;
revBinInt = revBinInt * 10 + remainder;
binaryInt = binaryInt / 10;
}
firstDigit += revBinInt/(int)(Math.pow(10,(int)Math.log(revBinInt)));
if(firstDigit == 0 && numOfDigits(revBinInt) <= 7){
binaryMsg += String.format("%8s", Integer.toString(revBinInt)).replace(' ', '0') + " ";
}
}
System.out.print(binaryMsg);
}
//Counts the number of digits in case binary number starts or ends in 0
public static int numOfDigits(int number){
int count = 0;
while(number !=0){
number = number/10;
count++;
}
return count;
}
}
Test Result:
Input: ABC, 4
Output: 01000101 00100011 01000111
Expected: 01000101 01000110 01000111