title: How to parse a Uuid stored as a char(16)?
body: <p>I have a Firebird database which stores Uuid values in a char(16) field. In my C# program, I need to retrieve these values in order to use them in subsequent queries. But the values I get back from the database are "garbage". (e.g. <code>¿­ñ)êNµmÏc—ÝX</code>) I've tried various methods of reading the data as a byte array and using that to create a local <code>Guid</code>, but that hasn't gotten me anywhere. The closest I've gotten was by using <code>Encoding.ASCII.GetBytes()</code> which did give me a "valid" guid, however it doesn't match the "real" guids in the database. I know they don't match because (1) the database has a UDF which converts the 'garbage' into a human readable string and (2) when I manually copy that human readable guid into my application, using it to create a new local <code>Guid</code>, and use THAT value in my queries, I get the correct results. (Whereas guids created from byte arrays do not yield correct results.) I also tried <code>IDataRecord.GetBytes()</code> but that resulted in an InvalidCastException ("Unable to cast object of type 'System.String' to type 'System.Byte[]'.") </p>
  
  <p>This application uses databases created by another, unrelated product and I have no control over its structure, nor can I use things like stored procedures. I also cannot use the UDF's I mentioned earlier because they are going bye-bye soon. The available UDF's apparently are simple wrappers around <a href="http://msdn.microsoft.com/en-us/library/aa379352%28v=vs.80%29.aspx" rel="nofollow noreferrer">UuidToString</a> and <a href="http://msdn.microsoft.com/en-us/library/aa379336%28VS.85%29.aspx" rel="nofollow noreferrer">UuidFromString</a>. I suppose I <em>could</em> use those functions in my own code, but I'd rather not if there's another way that doesn't involve interop.</p>
  
  <p>Ultimately, the I need the guid to work in a method that looks something like this:</p>
  
  <pre><code>protected DataTable QueryDataTable(string query, string paramName, Guid guid)
  {
      DataTable table = new DataTable();
      IDbCommand command = CreateDbCommand(query);
      if (command is FbCommand)
      {
          FbCommand fbCommand = (FbCommand)command;
          fbCommand.Parameters.Add(paramName, FbDbType.Binary).Value = 
              guid.ToByteArray();
  
          // Also tried passing in the byte[] generated by 
          // Encoding.Ascii.GetBytes.... didn't work.
          //fbCommand.Parameters.Add(paramName, FbDbType.Binary).Value = guid;
  
          FbDataAdapter adapter = new FbDataAdapter(fbCommand);
          adapter.Fill(table);
      }
  
      return table;
  }
  </code></pre>
  
  <p>Questions:</p>
  
  <p>(1) Why isn't my tactic of using a byte array to create a guid resulting in a "correct" guid?</p>
  
  <p>(2) What other tactics might I use to extract these guids and store them as locally as such?</p>
  
  <p>Thanks!</p>
  
  <h2>Edit:</h2>
  
  <p>Here's an example of what I've got in front of me. From the database:</p>
  
  <p><img src="https://i.stack.imgur.com/jZwkj.png" alt="enter image description here"></p>
  
  <p>Resulting char array from the garbage string:</p>
  
  <p><img src="https://i.stack.imgur.com/1zOd7.png" alt="enter image description here"></p>
  
  <p>Using the tactic suggested by @Alexei Levenkov, the resulting guid (1) is very close but not exactly correct (2).</p>
  
  <p>(1): <code>fca3120b-511e-4269-b88f-d053a34b3513</code><br>
  (2): <code>fca3120b-5184-4269-b88f-d053a34b3596</code></p>
  
  <p>SOME garbage strings do turn out correctly, but this is an example of one that does not. This is how I'm implementing Alexei's suggestion, as I didn't see a Select method for strings:</p>
  
  <pre><code>// table is a DataTable
  List&lt;byte&gt; bytes = new List&lt;byte&gt;();
  string blah = (string)table.Rows[0][0];
  foreach (char c in blah.ToCharArray())
  {
      bytes.Add((byte)c);
  }
  
  Guid guid = new Guid(bytes.ToArray());
  </code></pre>