package com.nuvole.poscom.util;
|
|
import java.nio.charset.Charset;
|
|
public class StringToGB18030 {
|
|
public static String toGB18030(String str) {
|
StringBuilder sb=new StringBuilder("0x");
|
Charset gb18030 = Charset.forName("GB18030");
|
byte[] bytes = str.getBytes(gb18030);
|
for (byte b : bytes) {
|
sb.append(Integer.toHexString(b & 0xFF).toUpperCase());
|
}
|
return sb.toString();
|
}
|
|
public static String parseGB18030(String strs) {
|
StringBuilder sb=new StringBuilder();
|
for (char c : strs.toCharArray()) {
|
sb.append(toGB18030(Character.toString(c)));
|
}
|
return sb.toString();
|
}
|
|
public static void main(String[] args) {
|
String s = "已收到";
|
System.out.println("转换后的字符串是:"+ StringToGB18030.parseGB18030(s));
|
}
|
}
|