UTF-16 Character Encoding of java

后端 未结 5 1146
借酒劲吻你
借酒劲吻你 2020-12-31 06:26

I was trying to understand character encoding in Java. Characters in Java are being stored in 16 bits using UTF-16 encoding. So while i am converting a string containing 6 c

5条回答
  •  庸人自扰
    2020-12-31 07:17

    As per the String.getBytes() method's documentation, the string is encoded into a sequence of bytes using the platform's default charset.

    I assume, your platform default charset will be ISO-8859-1 (or a similar one-byte-per-char-charset). These charsets will encode one character into one byte.

    If you want to specify the encoding, use the method String.getBytes(Charset) or String.getBytes(String).

    About the 16-bit storing: This is how Java internally stores characters, so also strings. It is based on the original Unicode specification.

提交回复
热议问题