【问题标题】:(Java) My hexadecimal decryption method can only decode the first hexadecimal value instead of the full string(Java) 我的十六进制解密方法只能解码第一个十六进制值而不是完整的字符串
【发布时间】:2021-11-09 01:31:08
【问题描述】:

我是一名初级程序员,目前正在学习 Java,但遇到了问题。

所以最近我一直在研究将字符串转换为十六进制值或从其转换为十六进制值的方法,例如“Hello”到 48656C6C6F,反之亦然。我将输入字符串转换为其十六进制值的方法效果很好,我已经对其进行了多次测试,但在完成之后我转向了它的反向方法,使用它我可以将十六进制值转换为字符串,但在几个错误之后,测试和重试,我仍然遇到同样的问题。

每当我在控制台中输入完整的十六进制代码时,它什么都不做,但是当我只输入单个字符的十六进制值(例如 z 的 7A)时,它工作得很好,除了符号,如 ', . , ; 等另外,由于 hexDeValue() 方法中的 switch 语句中的所有字符都具有最少 2 位的十六进制值,因此我尝试将输入字符串的子字符串与两个字符转换为常规字符。正如我所提到的,该方法的目的是将十六进制值转换为字符串,那么任何人都可以提出我应该修复的建议吗?

亲切的问候,

这是我的代码:

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

【问题讨论】:

  • hexDe 方法中的无限循环不是必需的。它只会让你的代码永远运行。
  • 请注意,您正在执行两个步骤:将十六进制转换为字节值,然后使用 ASCII 等编码将该字节转换为字符。最好分两步完成,注意 ASCII 已经在 char for Java 中实现:print((char) 0x48) 将打印 H,您可以使用 Integer 类解析十六进制,以及其他方式(但我想那是作弊)。

标签: java hex


【解决方案1】:

您的 hexDe() 包含 output = output + hexDeValue(textout); 行 - 但 hexDeValue(textout) 仅在 textout 包含单个字符的十六进制值(例如“41”)时才有效。

如果textout 包含例如“4142”,则只有hexDeValue() 中的默认大小写匹配并且hexDeValue(textout) 返回' '


还有更多问题:

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

表达式hext == "27" 仅在hext 被分配字符串常量“27”时才为真,但如果hext 是从某个较长的字符串中提取的,则不是(如您的情况)。请参阅How do I compare strings in Java? 了解更多信息。

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

这会从textout 中提取长度为 1 的子字符串。但以下代码仅在 hext 的长度为 2 时才有效。


您的代码的一个小问题是您在循环中反复连接字符串。这是非常低效的(虽然对于这么小的问题可能没关系),最好使用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();

如何修复代码

代替

output = output + hexDeValue(textout);

你可能是说

output = output + hexDeValue(hext);

为了正确解码字符,hext 的长度必须为 2。这意味着您需要在这之前写几行

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

但是如果i 等于textout.length()-1,这将导致IndexOutOfBoundsException。

我不知道您的代码处理输入中的空格字符有多重要。如果输入仅包含一对十六进制字符,那么您可以将循环重写为:

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();
}

如果你添加行

    case "27":
        return '\'';

对于hexDeValue() 中的switch 语句,您甚至不需要hexDe() 方法中"27" 的特殊情况。

【讨论】:

  • 你好;谢谢您的回答,我解决了您提到的一些问题,但是您能否建议我应该使用什么来代替我的 switch 语句,因为正如我所看到的那样,多个输入的数字被扔进默认情况表明我应该使用不同的东西。我之所以这么问,是因为到目前为止我在 Java 中学到的东西是我能想到的最好的。那么还有其他方法可以做到这一点吗?请让我知道,以便我可以研究这些方法并应用它们来更正我的代码。
  • @MarkoPlazmer8 我已经用可能的解决方案更新了我的答案
  • 你好@Thomas Kläger;感谢您的建议,我尽可能地修复了我的代码,但只有最后一个问题:我仍然遇到的越界异常。你能就如何解决这个问题提出建议吗?
猜你喜欢
  • 2018-08-21
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 2014-12-28
  • 2014-07-10
  • 1970-01-01
相关资源
最近更新 更多