java 16进制字符串转为二进制bit数组?

如题,
java 中16进制字符串转为二进制bit数组?
String str = "356A192B7913B04C54574D18C28D46E6395428AB";//16进制字符串40个
最后得到int [] bit = new int[40*4];
怎么得到bit二进制数组?
希望高手解答。

应该用byte型数组
public static String stringToHexString(String strPart) {
String hexString = "";
for (int i = 0; i < strPart.length(); i++) {
int ch = (int) strPart.charAt(i);
String strHex = Integer.toHexString(ch);
hexString = hexString + strHex;
}
return hexString;
}

private static String hexString="0123456789ABCDEF";
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(String str)
{
// 根据默认编码获取字节数组
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
// 将字节数组中每个字节拆解成2位16进制整数
for(int i=0;i<bytes.length;i++)
{
sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
}
return sb.toString();
}

/*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
// 将每2位16进制整数组装成一个字节
for(int i=0;i<bytes.length();i+=2)
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
return new String(baos.toByteArray());
}

private static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue();
byte ret = (byte) (_b0 | _b1);
return ret;

public static byte[] HexString2Bytes(String src)
{
byte[] ret = new byte[6];
byte[] tmp = src.getBytes();
for(int i=0; i<6; ++i )
{
ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
}
return ret;
}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网