title: Java UTF8 encoding
body: <p>I have a scenario in which some special characters are stored in a database (sybase) in the system's default encoding and I have to fetch this data and send it to a third-party in UTF-8 encoding using a Java program.</p>
  
  <p>There is precondition that the data sent to the third-party should not exceed a defined maximum size. Since upon conversion to UTF-8 a character may be replaced by 2 or 3 characters then my logic dictates that after getting the data from the database I must encode it into the UTF-8 string and then split the string. The following are my observations:</p>
  
  <p><em>When any special character like Chinese or Greek characters or any special character > ASCII 256 is encountered and when I convert it into UTF-8, a single character maybe represented by more than 1 byte.</em> </p>
  
  <p>So how can I be sure that the conversion is proper? For conversion I am using the following </p>
  
  <pre><code>// storing the data from database into string
  string s = getdata from the database;
  
  // converting all the data in byte array utf8 encoding
  byte [] b = s.getBytes("UTF-8");
  
  // creating a new string as my split logic is based on the string format
  
  String newString = new String(b,"UTF-8");
  </code></pre>
  
  <p>But when I output this newString to the console I get <code>?</code> for the special characters.</p>
  
  <p>So I have some doubts:</p>
  
  <ul>
  <li>If my conversion logic is wrong , then how could I correct it.</li>
  <li>After doing my conversion to UTF-8, can I double-check whether my conversion is OK or not? I mean is it the correct message which needs to be sent to the third-party, I assume that if the message is not user-readable after conversion then there is some problem with the conversion.</li>
  </ul>
  
  <p>Would like to have some points of view from all the experts out there.</p>
  
  <p>Please do let me know if any further info is needed from my side.</p>