【初心者】Java質問・相談スレッド87【大歓迎】
■ このスレッドは過去ログ倉庫に格納されています
0335デフォルトの名無しさん
2006/07/07(金) 20:55:25public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(toHexString("0123あいう"));
System.out.println(toHexString("0123あいう", "UTF8"));
}
private static final char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
public static String toHexString(String s) {
return toHexString(s.getBytes());
}
public static String toHexString(String s, String charsetName)
throws UnsupportedEncodingException {
return toHexString(s.getBytes(charsetName));
}
public static String toHexString(byte[] b) {
char[] cs = new char[b.length * 2];
for (int i = 0; i < b.length; i++) {
cs[i * 2] = hex[(b[i] >> 4) & 0xF];
cs[i * 2 + 1] = hex[(b[i] >> 0) & 0xF];
}
return new String(cs);
}
■ このスレッドは過去ログ倉庫に格納されています