Java:将十六进制字节数组转换为长度为16的固定二进制字符串。

huangapple go评论86阅读模式
英文:

Java: Convert HEX byte array to 16 fixed length binary String

问题

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)) {
        BigInteger bigInt = new BigInteger(s, 16);
        int length = s.length() * 4;  // Calculate the desired length of the binary string
        String binaryString = bigInt.toString(2);
        
        // Pad the binary string with leading zeros if necessary
        while (binaryString.length() < length) {
            binaryString = "0" + binaryString;
        }
        
        return binaryString;
    }

    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 "";
}
英文:

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 = &quot;0123456789ABCDEF&quot;.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 &quot;&quot;;
}
public static String bytesToHex(byte[] bytes) {
if (bytes != null &amp;&amp; bytes.length &gt; 0) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j &lt; bytes.length; j++) {
int v = bytes[j] &amp; 0xFF;
hexChars[j * 2] = HEX_ARRAY[v &gt;&gt;&gt; 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v &amp; 0x0F];
}
return new String(hexChars);
}
return &quot;&quot;;
}

答案1

得分: 2

使用 BigInteger 尝试一下:

byte[] bytes = {0, -56, 1};

String result = new BigInteger(bytes).setBit(16).toString(2).substring(1);
System.out.println(result);

输出结果为:

1100100000000001
英文:

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

huangapple
  • 本文由 发表于 2020年10月23日 19:26:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64499099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定