【问题标题】:Vertical Hex to ASCII Java垂直十六进制到 ASCII Java
【发布时间】:2017-10-24 06:43:18
【问题描述】:

情况:

我有一个 GUI,我已经将水平十六进制转换为 ASCII...但现在我想将垂直十六进制转换为 ASCII。

有没有人知道如何解决这个问题?

我已经拆分了:

vert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            fintext5.setText("");

            String[] test = entertext3.getText().split("\\n");
            for(int i = 0; i<test.length; i++){
                System.out.prinln(test[i]);
            }
         }
    });

水平十六进制代码:

button5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            fintext5.setText("");
            String readout = entertext3.getText().replace(" ", "").replace("\n", "");

            StringBuilder output = new StringBuilder("");               
            for (int i = 0; i < readout.length(); i += 2)
            {
                String str = readout.substring(i, i + 2);
                output.append((char) Integer.parseInt(str, 16));
            }
            fintext5.append(output.toString());
        }
    });

垂直十六进制示例(41 42 43 44 45 46):

4 4 4
1 2 3
4 4 4
4 5 6

【问题讨论】:

  • 你能举例说明这个“垂直十六进制”是什么吗?
  • 好吧,如果test[0].charAt(0) 是第一个 ASCII 的 first 数字,您认为在哪里可以找到它的 second 数字第一个 ASCII 码?
  • 也许是 test[1].chatAt(0) ?
  • 但是我如何在一个循环中得到这个?我不知道我会有多少行。
  • 你当然知道:test.length。唯一棘手的是您的外部循环需要以 2 为步长,例如 for (int i = 0; i &lt; test.length; i+=2)

标签: java type-conversion hex ascii


【解决方案1】:

你迷路了,我只是给你一个工作的起点:

vert.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
        ausgabe5.setText(""); 
        String[] test = eingabe3.getText().split("\\n"); 
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < test.length; i += 2) {
            for (int j = 0; j < test[i].length(); j++) {
                String hex = "" + test[i].charAt(j) + test[i + 1].charAt(j);
                output.append((char)Integer.parseInt(hex,16));
            }
        }
        System.out.println(output.toString());
    } 
}); 

请看一下,有什么不明白的地方问一下。

【讨论】:

  • 好的,非常感谢。我得到了我正在寻找的结果:D
  • 哦,我只有 1 个问题 parseInt 的 16 是做什么的?
  • 这是字符串的基数:16 表示十六进制
猜你喜欢
  • 1970-01-01
  • 2011-12-09
  • 2020-11-20
  • 2015-08-23
  • 2012-10-02
  • 2014-06-22
  • 2010-11-14
  • 1970-01-01
  • 2015-05-22
相关资源
最近更新 更多