Convert hexadecimal string (hex) to a binary string

后端 未结 7 2032
孤独总比滥情好
孤独总比滥情好 2020-11-30 07:31

I found the following way hex to binary conversion:

String binAddr = Integer.toBinaryString(Integer.parseInt(hexAddr, 16)); 

While this app

7条回答
  •  生来不讨喜
    2020-11-30 08:19

    public static byte[] hexToBin(String str)
        {
            int len = str.length();
            byte[] out = new byte[len / 2];
            int endIndx;
    
            for (int i = 0; i < len; i = i + 2)
            {
                endIndx = i + 2;
                if (endIndx > len)
                    endIndx = len - 1;
                out[i / 2] = (byte) Integer.parseInt(str.substring(i, endIndx), 16);
            }
            return out;
        }
    

提交回复
热议问题