最近在做一个开源的项目,里面涉及到了国际化,打开中文的配置文件,发现内容直接写成了Java Unicode的形式:/uXXXX的形式,没法看。顺手写了Unicode和文字相互转换的小程序,方便开发的进行。(程序没有健壮性,必须输入正确)
文字→Unicode
- package com.zsl;
- import java.util.Iterator;
- import java.util.Scanner;
- public class ZhToUnicode {
- public static void main(String[] args) {
- Scanner scanner=new Scanner(System.in);
- String str;
- char[] chars;
- while(true){
- str=scanner.next();
- chars=str.toCharArray();
- for (char c : chars) {
- if(c<256){
- System.out.print("\\u00"+Integer.toHexString(c));
- }else if(c<4096){
- System.out.print("\\u0"+Integer.toHexString(c));
- }else{
- System.out.print("\\u"+Integer.toHexString(c));
- }
- }
- System.out.println();
- System.out.flush();
- }
- }
- }
Unicode→文字
- package com.zsl;
- import java.io.UnsupportedEncodingException;
- import java.util.Scanner;
- public class UnicodeToZh {
- public static void main(String[] args) {
- Scanner scan=new Scanner(System.in);
- String unnicode;
- String[] words;
- while(true){
- unnicode=scan.next();
- words=unnicode.split("\\\\u");
-
- // for (String string : words) {
- // System.out.println(string);
- // }
- for (String string : words) {
-
- if((string!=null)&&(string.length()>2)){
- int tmp=Integer.parseInt(string, 16);
- System.out.print((char)tmp);
- // System.out.println(string);
- }
- }
- System.out.println();
- }
- }
- }
阅读(1054) | 评论(0) | 转发(0) |