[求助]请问字符串如何转换成16进制?
比如,String s = "adkh我34"; //“adkh我34”的16进制为“61 64 6B 68 CE D2 33 34”如何得到 String s1 = "61 64 6B 68 CE D2 33 34";
请问有什么方法吗?
[此贴子已经被作者于2007-4-12 14:42:30编辑过]
/**
* By [url=http://eastsun.javaeye.com]Eastsun[/url]
*/
public class EncodeString{
public static String encode(String str,String cs){
byte[] bs =null;
try{
bs =str.getBytes(cs);
}catch(Exception e){
return null;
}
String code =\"\";
for(int n=0;n<bs.length;n++) code += Integer.toHexString(bs[n]&0xff);
return code;
}
public static void main(String[] args){
System.out.println(\"GB2312编码: \"+encode(\"adkh我34\",\"gb2312\"));
System.out.println(\"UTF-8编码: \"+encode(\"adkh我34\",\"utf-8\"));
}
}