字符串转十六进制 十六进制转字符串

来源:互联网 发布:c 多线程编程 视频 编辑:程序博客网 时间:2024/06/05 02:49

//字符串转十六进制

String s="00011";

public static String toHexString(String s) 


String str=""; 
for (int i=0;i<s.length();i++) 

int ch = (int)s.charAt(i); 
String s4 = Integer.toHexString(ch); 
str = str + s4; 

return str; 

//十六进制转字符串


public static String toStringHex(String s) 

byte[] baKeyword = new byte[s.length()/2]; 
for(int i = 0; i < baKeyword.length; i++) 

try 

baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16)); 

catch(Exception e) 

e.printStackTrace(); 




try 

s = new String(baKeyword, "utf-8");//UTF-16le:Not 

catch (Exception e1) 

e1.printStackTrace(); 


return s; 
}

0 0
原创粉丝点击