자바에서 한글 <> 유니코드 변환
☆코딩개발 / 2013. 3. 22. 17:10
반응형
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));
}
}
반응형
'☆코딩개발' 카테고리의 다른 글
MSSQL 날짜 convert 형식(날짜 형식 변환) (0) | 2014.04.09 |
---|---|
자바 날짜 관련 로직 (0) | 2014.04.09 |
이클립스에서 import 할때 한글깨짐 현상 해결방법 (12) | 2013.03.26 |
이클립스에서 TODO 명령어를 사용해보자! (0) | 2013.03.22 |
자바에서 Date클래스로 현재날짜시간 구하는 방법 (0) | 2013.03.18 |
jQuery 제이쿼리란 무엇입니까 AJAX (0) | 2013.03.04 |