title: Handling Unicode surrogate values in Java strings
body: <p>Consider the following code:</p>
  
  <pre><code>byte aBytes[] = { (byte)0xff,0x01,0,0,
                    (byte)0xd9,(byte)0x65,
                    (byte)0x03,(byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07,
                    (byte)0x17,(byte)0x33, (byte)0x74, (byte)0x6f,
                     0, 1, 2, 3, 4, 5,
                     0 };
  String sCompressedBytes = new String(aBytes, "UTF-16");
  for (int i=0; i&lt;sCompressedBytes.length; i++) {
      System.out.println(Integer.toHexString(sCompressedBytes.codePointAt(i)));
  }
  </code></pre>
  
  <p>Gets the following incorrect output:</p>
  
  <pre><code>ff01, 0, fffd, 506, 717, 3374, 6f00, 102, 304, 500.
  </code></pre>
  
  <p>However, if the <code>0xd9</code> in the input data is changed to <code>0x9d</code>, then the following correct output is obtained:</p>
  
  <pre><code>ff01, 0, 9d65, 304, 506, 717, 3374, 6f00, 102, 304, 500.
  </code></pre>
  
  <p>I realize that the functionality is because of the fact that the byte <code>0xd9</code> is a high-surrogate Unicode marker. </p>
  
  <p>Question: Is there a way to feed, identify and extract surrogate bytes (<code>0xd800</code> to <code>0xdfff</code>) in a Java Unicode string?<br>
  Thanks </p>