十六进制与二进制字节数据相互转换

十六进制转换为字节数组

protected static byte[] hex2byte(byte[] b)

{

   if((b.length%2)!=0)

   {

       throw new IllegalArgumentException("des conver error!");

    }

    byte[] b2 = new byte[b.length/2];

    for(int n=0; n<b.length; n+=2)

    {

        String item = new String(b,n,2);

        b2[n/2] =(byte) Integer.parseInt(item,16);

     }

     return b2;

}

 

字节数组转换成十六进制字符串

protected static String byte2hex(byte[] b)

{

   String hs ="";

   String stmp="";

   for(byte bt: b)

   {

       stmp =(Integer.toHexString(b[n]&0XFF));

       if(stmp.length()==1)

       {

           hs=hs+"0"+stmp;

       }

       else

       {

           hs = hs +stmp;

       }

     }

    return hs.toUppercase();

}

 

你可能感兴趣的:(十六进制)