将字符串转换为字节数组

时间:2012-11-01 20:37:03

标签: java netbeans

我想将我的十六进制字符串转换为字节并将其保存到一个字节数组。我在互联网上看到了代码并尝试将其改编为我的程序。但我完全失去了。我对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 个答案:

答案 0 :(得分: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));
}

答案 1 :(得分:0)

您需要一个简单的实用工具类检查here。以下是我们从hex string转换为byte[]然后转换为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

答案 2 :(得分:0)

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

  public static void main(String[] args) {

    hexStringToByteArray(s);

     }

答案 3 :(得分:0)

我应该读得更好。我认为你的问题是转换,但我现在认为这是关于显示。如果它是关于显示器的,那么其他人已经注意到你没有在主方法中调用任何东西(这就是魔术发生的地方)。如果是转换,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);
}

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