使用 java.nio 和 java.nio.charset 中的类
-
java.nio.charset.Charset
-
java.nio.charset.CharsetEncoder
public static void main(String[] args) {
try {
String str = "\u7b80\u4f53\u4e2d\u6587";
Charset cs = Charset.forName("GB18030");
CharsetEncoder encoder = cs.newEncoder();
ByteBuffer bb = encoder.encode(CharBuffer.wrap(str));
byte[] bytes = bb.array();
for (int i = 0, n = bb.limit(); i < n ; i++) {
String hex = Integer.toHexString(bytes[i]);
System.out.println(hex.substring(hex.length()-2));
}
}catch (Exception e){
e.printStackTrace();
}
}
|
|