在Java中将字符串转换为UTF-16表示形式

Luk*_*uke 2 java string encoding utf-8 utf-16

我正在尝试将Java字符串转换为各种编码类型并将其打印出来.

例如,luke6C 75 6B 65UTF-8UTF-16而中国字符?将是E7 8C AAUTF-8732AUTF-16.

我该如何编写一个能够做到这一点的函数?

new String( org.apache.commons.codec.binary.Hex.encodeHex(str.getBytes("UTF-16")));
Run Code Online (Sandbox Code Playgroud)

似乎不起作用UTF-16.

Jin*_*won 7

public class UseTheForce {
    public static void main(final String[] args)
        throws java.io.UnsupportedEncodingException {
        for (final byte b : args[0].getBytes(args[1])) {
            System.out.printf("%1$02X ", (b & 0xFF));
        }
        System.out.println();
    }
}
Run Code Online (Sandbox Code Playgroud)

测试

$ java UseTheForce luke US-ASCII
6C 75 6B 65

$ java UseTheForce luke UTF-8
6C 75 6B 65

$ java UseTheForce luke UTF-16
FE FF 00 6C 00 75 00 6B 00 65

$ java UseTheForce luke UTF-16BE
00 6C 00 75 00 6B 00 65

$ java UseTheForce luke UTF-16LE
6C 00 75 00 6B 00 65 00

$ java UseTheForce luke UTF-32
00 00 00 6C 00 00 00 75 00 00 00 6B 00 00 00 65
Run Code Online (Sandbox Code Playgroud)

愿原力与你同在.

UPDATE

Formatter.html#detail中所述,该(b & 0xFF)部分不是必需的.