I'm trying to convert Java Strings into their various encoding types and print it out.

For example, luke would be 6C 75 6B 65 in UTF-8 and UTF-16 while the Chinese character would would be E7 8C AA in UTF-8 and 732A in UTF-16.

How do I write a function that does that?

new String( org.apache.commons.codec.binary.Hex.encodeHex(str.getBytes("UTF-16")));

doesn't seem to work for UTF-16.

有帮助吗?

解决方案

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();
    }
}

Test

$ 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

May the force be with you.

UPDATE

As describe in Formatter.html#detail, the (b & 0xFF) part is not necessary.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top