Created
April 26, 2012 12:39
-
-
Save sifue/2499294 to your computer and use it in GitHub Desktop.
JavaのUnicode文字列の変換用メソッド("あ" <-> "\u3042") ref: http://qiita.com/items/039846cf8415efdc5c92
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Unicode文字列に変換する("あ" -> "\u3042") | |
* @param original | |
* @return | |
*/ | |
private static String convertToUnicode(String original) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < original.length(); i++) { | |
sb.append(String.format("\\u%04X", Character.codePointAt(original, i))); | |
} | |
String unicode = sb.toString(); | |
return unicode; | |
} | |
/** | |
* Unicode文字列から元の文字列に変換する ("\u3042" -> "あ") | |
* @param unicode | |
* @return | |
*/ | |
private static String convertToOiginal(String unicode) | |
{ | |
String[] codeStrs = unicode.split("\\\\u"); | |
int[] codePoints = new int[codeStrs.length - 1]; // 最初が空文字なのでそれを抜かす | |
for (int i = 0; i < codePoints.length; i++) { | |
codePoints[i] = Integer.parseInt(codeStrs[i + 1], 16); | |
} | |
String encodedText = new String(codePoints, 0, codePoints.length); | |
return encodedText; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment