title: Escaping unicode characters with C/C++
body: <p>I need to escape unicode characters within a input string to either UTF-16 or UTF-32 escape sequences. For example, the input string literal <code>"Eat, drink, 愛"</code> should be escaped as <code>"Eat, drink, \u611b"</code>. Here are the rules in a table of sorts:</p>
  
  <blockquote>
    <p><strong>Escape</strong> |  <strong>Unicode code point</strong></p>
    
    <hr>
    
    <p>'\u' HEX HEX HEX HEX  | <em>A Unicode code point in the range U+0 to U+FFFF
    inclusive corresponding to the encoded hexadecimal value.</em></p>
    
    <hr>
    
    <p>'\U' HEX HEX HEX HEX HEX HEX HEX HEX |    <em>A Unicode code point in the range
    U+0 to U+10FFFF inclusive corresponding to the encoded hexadecimal</em>
    value.</p>
    
    <hr>
  </blockquote>
  
  <p>It's simple to detect Unicode characters in general, since the second byte is 0 if ASCII:</p>
  
  <pre><code>L"a" = 97, 0
  </code></pre>
  
  <p>, which will not be escaped. With Unicode characters the second byte is never 0:</p>
  
  <pre><code>L"愛" = 27, 97
  </code></pre>
  
  <p>, which is escaped as <code>\u611b</code>. But how do I detect UTF-32 a string as it is to be escaped differently than UTF-16 with 8 hex numbers?</p>
  
  <p>It is not as simple as just checking the size of the string, as UTF-16 characters are multibyte, e.g. :</p>
  
  <pre><code>L"प्रे" = 42, 9, 77, 9, 48, 9, 71, 9
  </code></pre>
  
  <p>I'm tasked to escape unescaped input string literals like <code>Eat, drink, 愛</code> and store them to disk in their escaped literal form <code>Eat, drink, \u611b</code> (UTF-16 example) If my program finds a UTF-32 character it should escape those too in the form<code>\U8902611b</code> (UTF-32 example), but I can't find a certain way of knowing if I'm dealing with UTF-16 or UTF-32 in an input byte array. So, just how can I reliably differ UTF-32 from UTF-16 characters within a <code>wchar_t</code> string or byte array?</p>