title: How do I read characters in a string as their UTF-32 decimal values?
body: <p>I have, for example, this Unicode string, which consists of the <a href="http://www.fileformat.info/info/unicode/char/1f300/index.htm" rel="noreferrer">Cyclone</a> and the <a href="http://www.fileformat.info/info/unicode/char/1f3ef/index.htm" rel="noreferrer">Japanese Castle</a> defined in C# and .NET, which uses UTF-16 for its CLR string encoding:</p>
  
  <pre><code>var value = "";
  </code></pre>
  
  <p>If you check this, you find very quickly that <code>value.Length = 4</code> because C# uses UTF-16 encoded strings, so for these reasons I can't just loop on each character and get its UTF-32 decimal value: <code>foreach (var character in value) result = (ulong)character;</code>. It begs the question, how can I get the UTF-32 decimal value for each character in any string?</p>
  
  <p>Cyclone should be <code>127744</code> and Japanese Castle should be <code>127983</code>, but I am looking for a general answer that can take any C# string and always produce a UTF-32 decimal value out of each character inside of it.</p>
  
  <p>I've even tried taking a look at <a href="https://msdn.microsoft.com/en-us/library/system.char.converttoutf32(v=vs.110).aspx" rel="noreferrer">Char.ConvertToUtf32</a>, but this seems to be problematic if, for example:</p>
  
  <pre><code>var value = "ac";
  </code></pre>
  
  <p>This has a length of 6. So, how do I know when a new character begins? For example:</p>
  
  <pre><code>Char.ConvertToUtf32(value, 0)   97  int
  Char.ConvertToUtf32(value, 1)   127744  int
  Char.ConvertToUtf32(value, 2)   'Char.ConvertToUtf32(value, 2)' threw an exception of type 'System.ArgumentException'   int {System.ArgumentException}
  Char.ConvertToUtf32(value, 3)   99  int
  Char.ConvertToUtf32(value, 4)   127983  int
  Char.ConvertToUtf32(value, 5)   'Char.ConvertToUtf32(value, 5)' threw an exception of type 'System.ArgumentException'   int {System.ArgumentException}
  </code></pre>
  
  <p>There is also the:</p>
  
  <pre><code>public static int ConvertToUtf32(
      char highSurrogate,
      char lowSurrogate
  )
  </code></pre>
  
  <p>But for me to use this as well I need to figure out when I have surrogate pairs. How can you do that?</p>