-1

我想将我的十六进制字符串转换为字节并将其保存到字节数组中。我在互联网上看到了代码并试图将其调整到我的程序中。但我完全不知所措。我对 java 很陌生。可以请帮助我进行转换。以下是我编写的代码。谢谢

public class Main {

    static String s = "00e83901c829e3735cc04137f3598a2b565c67665446d46ee74a6fd4ff8f556c7272fb6aeda45a757639aee558b130442fd4ff3f5cf98a08d0da6a23216d192dfd24bdda08a0b1081ae59fba0ae1516a2e02989df6b17a513b08895705552950e14fe430da3eae58fcc70619a129b534bbed6a9abc39706b1884b85a628781a86cc5223f038a7c0b48e1cf94033f7c5f1637900559b38fe2ccf41a14df5b8d81388fcebc69b59f7bc85a1c3e8b34a6deeb04a1e7fb1d3a7ae59009ea002aaa6ba5cdb9fa45653ac5eb89f61d436934f992197dbdb4c4a212cd7fbcd231debda57f11943b7f66215ecd616a4eed13fc9e38cd41d571b9faf496053b50a50321a076393ad91832959347f1fe5efd18e6267377108382fd992216a439ddc3dc59ce0ea955de95db767de4877caeaf8c7c4718e906d59db492cd610e7a28056f";

    public static void main(String[] args) {
    }

    private static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) +
                               Character.digit(s.charAt(i+1), 16));
        }
        return data;

    }
}
4

4 回答 4

2

如果您调用该方法,该代码将起作用。

public static void main(String[] args) {
    String s="00e83901c829e3735cc04137f3598a2b565c67665446d46ee74a6fd4ff8f556c7272fb6aeda45a757639aee558b130442fd4ff3f5cf98a08d0da6a23216d192dfd24bdda08a0b1081ae59fba0ae1516a2e02989df6b17a513b08895705552950e14fe430da3eae58fcc70619a129b534bbed6a9abc39706b1884b85a628781a86cc5223f038a7c0b48e1cf94033f7c5f1637900559b38fe2ccf41a14df5b8d81388fcebc69b59f7bc85a1c3e8b34a6deeb04a1e7fb1d3a7ae59009ea002aaa6ba5cdb9fa45653ac5eb89f61d436934f992197dbdb4c4a212cd7fbcd231debda57f11943b7f66215ecd616a4eed13fc9e38cd41d571b9faf496053b50a50321a076393ad91832959347f1fe5efd18e6267377108382fd992216a439ddc3dc59ce0ea955de95db767de4877caeaf8c7c4718e906d59db492cd610e7a28056f";
    byte[] b = hexStringToByteArray(s);
    System.out.println(b);

    //edit:
    System.out.println(Arrays.toString(b));
}
于 2012-11-01T20:45:01.660 回答
0

您需要一个简单的实用程序类检查下面是我们从hex stringto转换byte[]然后 to的示例,hex string然后我们正在比较以查看转换是否良好。

public class Test {

public static void main(String[] args) {
    String str = "00e83901c829e3735cc04137f3598a2b565c67665446d46ee74a6fd4ff8f556c7272fb6aeda45a757639aee558b130442fd4ff3f5cf98a08d0da6a23216d192dfd24bdda08a0b1081ae59fba0ae1516a2e02989df6b17a513b08895705552950e14fe430da3eae58fcc70619a129b534bbed6a9abc39706b1884b85a628781a86cc5223f038a7c0b48e1cf94033f7c5f1637900559b38fe2ccf41a14df5b8d81388fcebc69b59f7bc85a1c3e8b34a6deeb04a1e7fb1d3a7ae59009ea002aaa6ba5cdb9fa45653ac5eb89f61d436934f992197dbdb4c4a212cd7fbcd231debda57f11943b7f66215ecd616a4eed13fc9e38cd41d571b9faf496053b50a50321a076393ad91832959347f1fe5efd18e6267377108382fd992216a439ddc3dc59ce0ea955de95db767de4877caeaf8c7c4718e906d59db492cd610e7a28056f";
    String str1 = new String(encodeHex(hexStringToByteArray(str)));
    if (str1.equals(str)) {
        System.out.println("String matches ");
    }
}

public static byte[] hexStringToByteArray(String str) {

    char[] data = str.toCharArray();
    int len = data.length;
    byte[] out = new byte[len >> 1];
    for (int i = 0, j = 0; j < len; i++) {
        int f = Character.digit(data[j], 16) << 4;
        j++;
        f = f | Character.digit(data[j], 16);
        j++;
        out[i] = (byte) (f & 0xFF);
    }
    return out;
}

public static char[] encodeHex(byte[] data) {

    int l = data.length;
    char[] out = new char[l << 1];
    for (int i = 0, j = 0; i < l; i++) {
        out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
        out[j++] = DIGITS[0x0F & data[i]];
    }

    return out;
}

private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6',
        '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}

输出:

 String matches
于 2012-11-01T20:41:13.767 回答
0

当您运行 java 程序时,它调用 main 方法,在您的情况下它是空的。您必须在 main 方法中调用您的方法。由于您有静态 hexStringToByteArray 方法,您可以这样调用它

  public static void main(String[] args) {

    hexStringToByteArray(s);

     }
于 2012-11-01T20:50:23.647 回答
0

我应该读得更好。我以为你的问题是关于转换的,但我现在认为是关于显示的。如果是关于显示,这里的其他人已经注意到你没有在你的 main 方法中调用任何东西(这是魔法发生的地方)。如果是关于转换,BigInterger 可以为您完成繁重的工作。以防万一:这将起作用(最后 5 行仅用于检查输出是否与输入匹配)。

public static void main(String[] args) {
    byte[] bytes = new BigInteger(s, 16).toByteArray();
    System.out.println(Arrays.toString(bytes));

    List<String> hexToCheck = new ArrayList<String>(bytes.length);
    for (byte b : bytes) {
        hexToCheck.add(String.format("%02X", b));
    }
    System.out.println(hexToCheck);
}

(如果您的问题的正确答案是填写主要方法,请选择其他答案之一作为正确答案)

于 2012-11-01T21:49:43.930 回答