I'm a beginner programmer who is currently learning Java and I ran into a problem.
So recently I've been working on methods for converting Strings to or from their hexadecimal values, for instance "Hello" to 48656C6C6F, and vice versa. My method for converting the input String to its hexadecimal value works well, and I've tested it several times, but after having finished it I moved on to its reverse method, using which I could convert hexadecimal values to Strings, but after several errors, tests and re-tries, I'm still stuck with the same problem.
Whenever I input a full hexadecimal code into the console, it does nothing, but when I only input the hexadecimal value of a single character (for example 7A for z), it works well, except for symbols such as ', ., ;, etc. Also, since all of the characters in my switch statement in the hexDeValue() method have hexadecimal values with minimum 2 digits, I tried to take substrings of the input String with two characters to be converted into regular characters. As I mentioned, the purpose of the method is to convert hexadecimal values into Strings, so could anyone make a suggestion of what I should fix?
Kind regards,
Here is my code:
public class HexDecode {
public static void main(String[] args) throws InterruptedException {
while(true) {
System.out.println("Enter a hexadecimal value");
String input = TextIO.getlnString();
System.out.println(hexDe(input));
}
}
public static String hexDe(String textout) {
String output = "";
String invertedcomma = "'";
while (true) {
for(int i = 0; i < textout.length(); i++) {
int j = i+1;
String hext = textout.substring(i, j);
char smolhext = textout.charAt(i);
if (smolhext == ' ' ) {
output = output + ' ';
}
if(i%2 == 0) {
if (hext == "27") {
output = output + invertedcomma;
}
else {
output = output + hexDeValue(textout);
}
if (hexDeValue(hext) == ' ') {
output = output + " ";
}
if (j > textout.length()) {
return output;
}
}
}
return output;
}
}
public static char hexDeValue(String parameter) {
switch(parameter) {
case "21":
return '!';
case "22":
return '"';
case "23":
return '#';
case "24":
return '$';
case "25":
return '%';
case "26":
return '&';
case "28":
return '(';
case "29":
return ')';
case "2A":
return '*';
case "2B":
return '+';
case "2C":
return ',';
case "2D":
return '-';
case "2E":
return '.';
case "2F":
return '/';
case "30":
return '0';
case "31":
return '1';
case "32":
return '2';
case "33":
return '3';
case "34":
return '4';
case "35":
return '5';
case "36":
return '6';
case "37":
return '7';
case "38":
return '8';
case "39":
return '9';
case "3A":
return ':';
case "3B":
return ';';
case "3C":
return '<';
case "3D":
return '=';
case "3E":
return '>';
case "3F":
return '?';
case "40":
return '@';
case "41":
return 'A';
case "42":
return 'B';
case "43":
return 'C';
case "44":
return 'D';
case "45":
return 'E';
case "46":
return 'F';
case "47":
return 'G';
case "48":
return 'H';
case "49":
return 'I';
case "4A":
return 'J';
case "4B":
return 'K';
case "4C":
return 'L';
case "4D":
return 'M';
case "4E":
return 'N';
case "4F":
return 'O';
case "50":
return 'P';
case "51":
return 'Q';
case "52":
return 'R';
case "53":
return 'S';
case "54":
return 'T';
case "55":
return 'U';
case "56":
return 'V';
case "57":
return 'W';
case "58":
return 'X';
case "59":
return 'Y';
case "5A":
return 'Z';
case "5B":
return '[';
case "5D":
return ']';
case "5E":
return '^';
case "5F":
return '_';
case "60":
return '`';
case "61":
return 'a';
case "62":
return 'b';
case "63":
return 'c';
case "64":
return 'd';
case "65":
return 'e';
case "66":
return 'f';
case "67":
return 'g';
case "68":
return 'h';
case "69":
return 'i';
case "6A":
return 'j';
case "6B":
return 'k';
case "6C":
return 'l';
case "6D":
return 'm';
case "6E":
return 'n';
case "6F":
return 'o';
case "70":
return 'p';
case "71":
return 'q';
case "72":
return 'r';
case "73":
return 's';
case "74":
return 't';
case "75":
return 'u';
case "76":
return 'v';
case "77":
return 'w';
case "78":
return 'x';
case "79":
return 'y';
case "7A":
return 'z';
case "7B":
return '{';
case "7C":
return '|';
case "7D":
return '}';
case "7E":
return '~';
default:
return ' ';
}
}
}
enter code here