`
learnmore
  • 浏览: 588941 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

十六进制转字符串

    博客分类:
  • java
 
阅读更多
记录一下十六进制转字符串,以备以后使用方便
//十六进制转字符串utf-8
public static String hexToString(String hex) throws UnsupportedEncodingException
	{
	      byte[] b=new byte[hex.length()/2];
	      for(int i=0;i<b.length;i++)
	     {
	         b[i]=(byte)(0xff&Integer.parseInt(hex.substring(i*2, i*2+2), 16));
	     }
	     return new String(b,"utf-8");
}


//字符串转十六进制utf-8
public static String stringToHex(String s)
	{
		String str="";
		byte[] b=s.getBytes(Charset.forName("utf-8"));
	    for(int i=0;i<b.length;i++)
	    {
	    	str+=Integer.toHexString(b[i]&0xff);
	    }
	    return str;
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics