String tempStr="中文";//准备转换的字符
成都创新互联提供高防主机、云服务器、香港服务器、服务器托管等
String result=new String(tempStr.getBytes("GB2312"),"UTF-8");//转换后的结果
可以调用show("在国",16)生成16进制,17、22........,最多支持到36
import java.util.ArrayList;
import java.util.List; public class ThritySix { /**
* @param args
*/
public static void main(String[] args) {
String str = "中华人发共和国";
System.out.println(show(str,36));
} public static List show(String source, int p) {
List list = new ArrayList();
char[] arr = source.toCharArray();
for (char c : arr) {
StringBuilder sb = new StringBuilder();
int tt = c; int ii = 0;
System.out.println(tt);
while (tt != 0) {
ii = tt % p;
if (ii 9) {//如果大于9则用A-Z代替
sb.insert(0, getData(ii));//插入到最前面
} else {
sb.insert(0, ii);//插入到最前面
}
tt = tt / p;
}
list.add(sb.toString());
}
return list;
}
上代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入汉字:");
String input = sc.nextLine();
if (input.length() == 0) {
System.out.println("输入错误");
return;
}
System.out.println("汉字转unicode结果:");
char[] chars = input.toCharArray();
for (int i = 0; i chars.length; i++) {
String unicode = Integer.toHexString(chars[i]);
if (unicode.length() = 2) {
// 不足四位前面加0补齐
unicode = "00" + unicode;
}
unicode = "\\u" + unicode;
System.out.println(chars[i] + ": " + unicode);
}
}
}
把INPUT改成想转换的汉字
import java.io.UnsupportedEncodingException;
public class SoNaiveTooSimple {
public static final String INPUT = "我爱你,妈妈";
public static void main(String[] args) throws UnsupportedEncodingException {
byte[] input = INPUT.getBytes("UNICODE");
for (int i = 2; i input.length; i += 2) {
int result = ((input[i] 0xff) * 16 * 16) + (input[i + 1] 0xff);
System.out.print("" + result + ";");
}
}
}