简体   繁体   中英

Can any character be encoded in UTF-16 (using Java 8)

Can any character be encoded in UTF-16 (using java) ?

I thought it could but my code that encodes as

CharsetEncoder encoder = Charset.forName("UTF-16LE").newEncoder();
ByteBuffer bb = encoder.encode(CharBuffer.wrap((String) value + '\0'));

has thrown a CharacterCodingException

Unfortunately as this only occurred for a customer not myself I dont have details of the offending character.

There are possible values of char that are not valid UTF-16 sequences. For example:

    CharsetEncoder encoder = Charset.forName("UTF-16LE").newEncoder();
    ByteBuffer bb = encoder.encode(CharBuffer.wrap("\uDFFF"));

This code will throw an exception. U+DFFF is an unpaired surrogate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM