I have an UTF-8
string and I need to get the byte array of UTF-16
encoding, so how can I convert my string to UTF-16
byte array?
Update:
I mean we have Encoding.Unicode.GetBytes()
or even Encoding.UTF8.GetBytes()
function to get byte array of strings, what about UTF-16
? We don't have any Encoding.UTF16.GetBytes()
so how can I get the byte array?
UTF-16 (16- bit Unicode Transformation Format) is a standard method of encoding Unicode character data. Part of the Unicode Standard version 3.0 (and higher-numbered versions), UTF-16 has the capacity to encode all currently defined Unicode characters.
UTF-16 is based on 16-bit code units. Each character is encoded as at least 2 bytes. Some characters that are encoded with a 1-byte code unit in UTF-8 are encoded with a 2-byte code unit in UTF-16. Characters that are surrogate or supplementary characters use 4 bytes and thus require additional storage.
UTF-8 is a byte encoding used to encode unicode characters. UTF-8 uses 1, 2, 3 or 4 bytes to represent a unicode character. Remember, a unicode character is represented by a unicode code point. Thus, UTF-8 uses 1, 2, 3 or 4 bytes to represent a unicode code point.
Convert byte[] to String (text data) toString() to get the string from the bytes; The bytes. toString() only returns the address of the object in memory, NOT converting byte[] to a string ! The correct way to convert byte[] to string is new String(bytes, StandardCharsets. UTF_8) .
For little-endian UTF-16, use Encoding.Unicode
.
For big-endian UTF-16, use Encoding.BigEndianUnicode
.
Alternatively, construct an explicit instance of UnicodeEncoding
which allows you to specify the endianness, whether or not to include byte-order marks, and whether to throw an exception on invalid data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With