title: Converting an NSString to and from UTF32
body: <p>I'm working with a database that includes hex codes for UTF32 characters. I would like to take these characters and store them in an NSString. I need to have routines to convert in both ways.</p>
  
  <p>To convert the first character of an NSString to a unicode value, this routine seems to work:</p>
  
  <pre><code>const unsigned char *cs = (const unsigned char *)
      [s cStringUsingEncoding:NSUTF32StringEncoding];
  uint32_t code = 0;
  for ( int i = 3 ; i &gt;= 0 ; i-- ) {
      code &lt;&lt;= 8;
      code += cs[i];
  }
  return code;
  </code></pre>
  
  <p>However, I am unable to do the reverse (i.e. take a single code and convert it into an NSString). I thought I could just do the reverse of what I do above by simply creating a c-string with the UTF32 character in it with the bytes in the correct order, and then create an NSString from that using the correct encoding.</p>
  
  <p>However, converting to / from cstrings does not seem to be reversible for me.</p>
  
  <p>For example, I've tried this code, and the "tmp" string is not equal to the original string "s".</p>
  
  <pre><code>char *cs = [s cStringUsingEncoding:NSUTF32StringEncoding];
  NSString *tmp = [NSString stringWithCString:cs encoding:NSUTF32StringEncoding];
  </code></pre>
  
  <p>Does anyone know what I am doing wrong? Should I be using "wchar_t" for the cstring instead of char *?</p>
  
  <p>Any help is greatly appreciated!</p>
  
  <p>Thanks,
  Ron</p>