JAVA
자바 한글 UNICODE 변환 (특수문자안됨)
ndlessrain
2013. 1. 16. 17:44
728x90
가끔 프로젝트하다 유니코드 인코딩 디코딩할떄가 생겨서
그냥 여기저기서 짜집기해서 만들었다......
public class Unicode {
public static String decode(String unicode)throws Exception {
StringBuffer str = new StringBuffer();
char ch = 0;
for( int i= unicode.indexOf("\\u"); i > -1; i = unicode.indexOf("\\u") ){
ch = (char)Integer.parseInt( unicode.substring( i + 2, i + 6 ) ,16);
str.append( unicode.substring(0, i) );
str.append( String.valueOf(ch) );
unicode = unicode.substring(i + 6);
}
str.append( unicode );
return str.toString();
}
public static String encode(String unicode)throws Exception {
StringBuffer str = new StringBuffer();
for (int i = 0; i < unicode.length(); i++) {
if(((int) unicode.charAt(i) == 32)) {
str.append(" ");
continue;
}
str.append("\\u");
str.append(Integer.toHexString((int) unicode.charAt(i)));
}
return str.toString();
}
public static void main(String[] args) throws Exception {
String str = encode("한 글");
System.out.println(str);
System.out.println(decode(str));
}
}
728x90