![]() ![]() Let’s create a CharArray, and then convert as described. To convert a CharArray to a UTF-8 String, we can convert it to an encoded ByteBuffer first, then decode it to a CharBuffer, and finally turn it into a String. These are some of the reasons UTF-8 is commonly used and recommended. Legacy programs can usually handle UTF-8 files even if they have some UTF-8 characters. In addition to UTF-8’s lower memory consumption, it’s also ASCII compatible, representing ASCII characters the same way ASCII does. UTF-16 uses at least 16 bits (2 bytes), and UTF-32 always uses 32 bits. UTF-8 uses at least 1 byte, or 8 bits, to represent a character, hence the name. Many other special characters are similar, especially in non-English languages. ![]() UTF-8 is a Unicode encoding while basic ASCII isn’t, and ignoring extended ASCII, representing Unicode characters like the Euro symbol requires something more advanced. Val asciiString = String(byteArray, Charsets.US_ASCII)Īssertions.assertEquals(originalString, utf8String)Īssertions.assertNotEquals(originalString, asciiString)Īssertions.assertEquals("That will cost ���10.", asciiString)Īs we can see, certain characters cannot be represented accurately in basic ASCII. Val utf8String = String(byteArray, Charsets.UTF_8) Val byteArray = originalString.toByteArray() Val originalString = "That will cost €10." ![]()
0 Comments
Leave a Reply. |