Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetDecoder.html" rel="noreferrer"><code>CharsetDecoder</code></a> should be what you are looking for, no ?</p> <p>Many network protocols and files store their characters with a byte-oriented character set such as <code>ISO-8859-1</code> (<code>ISO-Latin-1</code>).<br> However, Java's native character encoding is <a href="http://www.joelonsoftware.com/articles/Unicode.html" rel="noreferrer"><del>Unicode</del></a> UTF16BE (Sixteen-bit UCS Transformation Format, big-endian byte order).</p> <p>See <a href="http://download-llnw.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html" rel="noreferrer"><code>Charset</code></a>. That doesn't mean <code>UTF16</code> is the default charset (i.e.: the default "mapping between sequences of sixteen-bit <strong><a href="http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Character.html#unicode" rel="noreferrer">Unicode code units</a></strong> and sequences of bytes"):</p> <blockquote> <p>Every instance of the Java virtual machine has a default charset, which may or may not be one of the standard charsets.<br> [<code>US-ASCII</code>, <code>ISO-8859-1</code> a.k.a. <code>ISO-LATIN-1</code>, <code>UTF-8</code>, <code>UTF-16BE</code>, <code>UTF-16LE</code>, <code>UTF-16</code>]<br> The default charset is determined during virtual-machine startup and typically depends upon the locale and charset being used by the underlying operating system.</p> </blockquote> <p>This example demonstrates how to convert <code>ISO-8859-1</code> encoded bytes in a <code>ByteBuffer</code> to a string in a <code>CharBuffer</code> and visa versa.</p> <pre><code>// Create the encoder and decoder for ISO-8859-1 Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); try { // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer // The new ByteBuffer is ready to be read. ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string")); // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string. // The new ByteBuffer is ready to be read. CharBuffer cbuf = decoder.decode(bbuf); String s = cbuf.toString(); } catch (CharacterCodingException e) { } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload