0

I have a byte array obtained by automatically MYBATIS conversion. Into ORACLE DB column (RAW type) I have HEX value (example: 0x0, 0x81, 0xC801). This value is stored with maximum 2 bytes (example 1100100000000001 -> 0xC801, 0000000000000000 -> 0x0)

I have a problem when i read and convert this value.

I need to convert the byte array obtained by automatically conversion, to 16 fixed length binary string.

For example if the value in DB is 0x0:

  • byte array is [0] (automatically MYBATIS conversion)
  • binary string that I need must be 0000000000000000

For example if the value in DB is 0xC801:

  • byte array is [0, -56, 1] (automatically MYBATIS conversion)
  • binary string that I need must be 1100100000000001

How i can do that?

I tried with this solution but doesn't work with HEX value like 0x0 or 0x81 because 0x0 is mapped to "0" and 0x81 is mapped to "100000001". 0 padding is missing... and i don't know how to add the padding insite this script.

private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

public static String bytesToBinary(byte[] bytes) {
    return hexToBin(bytesToHex(bytes));
}

public static String hexToBin(String s) {

    if (StringUtils.isNotEmpty(s)) {
        return new BigInteger(s, 16).toString(2);
    }

    return "";
}

public static String bytesToHex(byte[] bytes) {

    if (bytes != null && bytes.length > 0) {

        char[] hexChars = new char[bytes.length * 2];

        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }

        return new String(hexChars);
    }

    return "";
}
6
  • Do you always want to convert just two bytes. Are you always returned a 3 byte array? Commented Oct 23, 2020 at 12:04
  • @WJS Read the question again. "byte array is [0]" and "byte array is [0, -56, 1]". Question has 2 examples, of length 1 and 3, respectively, so no, it is not always just two bytes. Commented Oct 23, 2020 at 12:08
  • @Andreas The title says convert to a 16 bit string. That implies 2 bytes to me. Commented Oct 23, 2020 at 12:09
  • @WJS Yes, and the question text shows 2 examples of byte arrays with 1 and 3 bytes. Since the first byte is 0, that is still at most a 16 bit value. Commented Oct 23, 2020 at 12:11
  • Which is what I said. You only want to convert at most two bytes (in spite of the array size). Commented Oct 23, 2020 at 12:12

1 Answer 1

2

Try using BigInteger

byte[] bytes = {0, -56, 1};
        
                              // array, offset, length 
String result = new BigInteger(bytes).setBit(16).toString(2).substring(1);
System.out.println(result);

Prints

1100100000000001
Sign up to request clarification or add additional context in comments.

4 Comments

Close, but with input {0} the output is 0, not the requested 0000000000000000. Can be fixed e.g. as follows: new BigInteger(bytes).setBit(16).toString(2).substring(1)
If you use new BigInteger(bytes) instead of new BigInteger(bytes,0,3), then the code can handle any byte array of length 1-4, instead of only handling arrays of length 3.
Also, the BigInteger​(byte[] val, int off, int len) constructor wasn't even added until Java 9, so by changing to the BigInteger​(byte[] val) constructor it'll work for all Java versions (well, Java 1.1+).
Thank you very much guys! SOLVED with your solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.