`
jinghwm
  • 浏览: 26133 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

 
阅读更多
十六进制转换为字节数组

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();

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics