将字符串转换为字节数组。

时间:2022-09-01 12:30:49

I want to convert my hex string to byte and save it to a byte array.I saw the code on the internet and tried to adapt it to my program.But i am at a complete loss.I am quite new to java.Could some one please help me in doing my conversion.Below is the code i wrote. Thank you

我想将我的十六进制字符串转换成字节,并将它保存到一个字节数组中。我在网上看到了这段代码,并试着让它适应我的程序。但我完全失去了。我对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 个解决方案

#1


2  

The code works if you call the method.

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

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

    //edit:
    System.out.println(Arrays.toString(b));
}

#2


0  

When you run java program its call to main method and in your case which is empty. you have to call your methods in main method. Since you have static hexStringToByteArray method you can call it like this

当您运行java程序时,它调用main方法,而在您的情况中是空的。你必须在主方法中调用你的方法。因为你有静态的hexStringToByteArray方法,你可以这样称呼它。

  public static void main(String[] args) {

    hexStringToByteArray(s);

     }

#3


0  

You need a simple utility class check here. Below is the example where we are converting from hex string to byte[] and then to hex string and then we are comparing to see if conversion was good.

这里需要一个简单的实用程序类检查。下面是我们将十六进制字符串转换为字节[],然后转换为十六进制字符串的例子,然后我们比较看看转换是否良好。

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' };
}

Output:

输出:

 String matches

#4


0  

I should be reading better. I thought your question was about the conversion, but I now think it's about the display. If it's about the display, others here have noted that you're not calling anything in your main method (which is where the magic happens). If it's about the conversion, BigInterger can do the heavy lifting for you. Just in case: this will work (last 5 lines are just to check if the output matches the input).

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

(if the correct answer to your question is filling the main method, please choose one of the other responses as the right answer)

(如果你的问题的正确答案填满了主要的方法,请选择另一个回答作为正确答案)

#1


2  

The code works if you call the method.

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

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

    //edit:
    System.out.println(Arrays.toString(b));
}

#2


0  

When you run java program its call to main method and in your case which is empty. you have to call your methods in main method. Since you have static hexStringToByteArray method you can call it like this

当您运行java程序时,它调用main方法,而在您的情况中是空的。你必须在主方法中调用你的方法。因为你有静态的hexStringToByteArray方法,你可以这样称呼它。

  public static void main(String[] args) {

    hexStringToByteArray(s);

     }

#3


0  

You need a simple utility class check here. Below is the example where we are converting from hex string to byte[] and then to hex string and then we are comparing to see if conversion was good.

这里需要一个简单的实用程序类检查。下面是我们将十六进制字符串转换为字节[],然后转换为十六进制字符串的例子,然后我们比较看看转换是否良好。

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' };
}

Output:

输出:

 String matches

#4


0  

I should be reading better. I thought your question was about the conversion, but I now think it's about the display. If it's about the display, others here have noted that you're not calling anything in your main method (which is where the magic happens). If it's about the conversion, BigInterger can do the heavy lifting for you. Just in case: this will work (last 5 lines are just to check if the output matches the input).

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

(if the correct answer to your question is filling the main method, please choose one of the other responses as the right answer)

(如果你的问题的正确答案填满了主要的方法,请选择另一个回答作为正确答案)