Skip to content

Instantly share code, notes, and snippets.

@sifue
Created April 26, 2012 12:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sifue/2499294 to your computer and use it in GitHub Desktop.
Save sifue/2499294 to your computer and use it in GitHub Desktop.
JavaのUnicode文字列の変換用メソッド("あ" <-> "\u3042") ref: http://qiita.com/items/039846cf8415efdc5c92
/**
* 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