0

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
  • The infinite loop inside hexDe method is not required. It just makes your code run forever. – Abishek Stephen Sep 13 '21 at 19:29
  • Do note that you are performing two steps as one: convert hex to a byte value, and then using an encoding such as ASCII to convert that byte to a character. It's much better to do those in two steps, and note that ASCII is already implemented in `char` for Java: `print((char) 0x48)` will print `H`, and you can parse hexadecimals using the `Integer` class, amongst other ways (but I suppose that's cheating). – Maarten Bodewes Sep 14 '21 at 22:02

1 Answers1

0

Your hexDe() contains the line output = output + hexDeValue(textout); - but hexDeValue(textout) only works if textout contains the hex value for a single character (for example "41").

If textout contains for example "4142" then only the default case in hexDeValue() matches and hexDeValue(textout) returns ' '.


There are more problems:

if (hext == "27") {
    output = output + invertedcomma;
}

The expression hext == "27" will only be true if hext is assigned the string constant "27", but not (as in your case) if hext is extracted from some longer string. See How do I compare strings in Java? for more information.

int j = i+1;
String hext = textout.substring(i, j);

This extracts a substring of length 1 from textout. But the following code only works if hext has a length of 2.


A minor problem with your code is that you repeatedly concatenate strings in a loop. This is very inefficient (although it probably doesn't matter for such a small scale problem), it would be better to use a StringBuilder:

    StringBuilder output = new StringBuilder();

    // instead of output = output + something; use
    output.append(something);

    // at the end of your method, instead of return output; use
    return output.toString();

How to fix the code

Instead of

output = output + hexDeValue(textout);

you probably meant

output = output + hexDeValue(hext);

For this to properly decode character it is required that hext has a length of 2. That means that a few lines before that you need to write

int j = i+2;
String hext = textout.substring(i, j);

but this will lead to an IndexOutOfBoundsException if i is equal to textout.length()-1.

I don't know how important it is for your code to handle space characters in the input. If the input only consists of pairs of hexadecimal characters then you could rewrite your loop as:

public static String hexDe(String textout) {
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < textout.length(); i += 2) {
        String hext = textout.substring(i, i+2);
        if (hext.equals("27")) {
            output.append(invertedcomma);
        }
        else {
            output.append(hexDeValue(hext));
        }
    }
    return output.toString();
}

and if you add the lines

    case "27":
        return '\'';

to the switch statement in hexDeValue() you don't even need the special case for "27" in the hexDe() method.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • Hello; thank you for your answer, I fixed some of the problems that you mentioned, but could you please give a suggestion of what I should use instead of my switch statement, because as I can see, the fact that the multiple entered digits are thrown into the default case shows that I should use something different. I'm asking because from what I've learnt in Java so far this is the best I could come up with. So are there any other ways to do this? Please let me know so that I could study those ways and apply them to correct my code. – MarkoPlazmer8 Sep 14 '21 at 16:15
  • @MarkoPlazmer8 I've updated my answer with a possible solution – Thomas Kläger Sep 14 '21 at 21:35
  • Hello @Thomas Kläger ; Thank you for your advice, I fixed my code as much as I could, but there is just one last problem: the out of bounds exception that I'm still experiencing. Could you make a suggestion on how to fix that? – MarkoPlazmer8 Sep 17 '21 at 16:13