Java Coder
How do I initialize a byte array in Java?
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners

How do I initialize a byte array in Java?

Advertisement
View Answers

December 24, 2013 at 3:59 PM


byte[] CDRIVES = hexStringToByteArray("e04fd020ea3a6910a2d808002b30309d");

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

Java convert file to byte array : Example Java Array Initialization: Example










Advertisement