80,351
社区成员
发帖
与我相关
我的任务
分享
try {
String str = "暂";
byte[] data = str.getBytes();
System.out.print("getBytes():Default ");
for (int i = 0; i < data.length; i++) {
System.out.print(Integer.toHexString(data[i]) + " ");
}
System.out.println();
data = str.getBytes("utf-8");
System.out.print("getBytes():UTF-8 ");
for (int i = 0; i < data.length; i++) {
System.out.print(Integer.toHexString(data[i]) + " ");
}
System.out.println();
data = str.getBytes("gbk");
System.out.print("getBytes():gbk ");
for (int i = 0; i < data.length; i++) {
System.out.print(Integer.toHexString(data[i]) + " ");
}
System.out.println();
data = str.getBytes("gb2312");
System.out.print("getBytes():gb2312 ");
for (int i = 0; i < data.length; i++) {
System.out.print(Integer.toHexString(data[i]) + " ");
}
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
getBytes():Default ffffffe6 ffffff9a ffffff82
getBytes():UTF-8 ffffffe6 ffffff9a ffffff82
getBytes():gbk ffffffd4 ffffffdd
getBytes():gb2312 ffffffd4 ffffffdd
public String chineseToHexString(String str){
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
if(str==null || str.length()<=0){
return "";
}
byte[] a=str.getBytes();
StringBuffer sb = new StringBuffer();
for(int i=0; i<a.length; i++){
sb.append(Integer.toHexString((256+a[i])%256) + " ");
}
return sb.toString();
}