Note that there are some explanatory texts on larger screens.

plurals
  1. PObase64 to guid to base64
    text
    copied!<p>I'm currently researching MongoDb as a possible database option, and I'm having trouble dealing with Guid serialization. I thought at first maybe this was a bug in the C# driver's serialization, but now I think it's more likely a naive assumption on my part.</p> <p>To help me convert the Bson base64 representations back and forth to Guids, I wrote a couple of little powershell functions to help:</p> <pre><code>function base64toguid { param($str); $b = [System.Convert]::FromBase64String($str); $hex = ""; foreach ($x in $b) { $hex += $x.ToString("x2"); } $g = new-object -TypeName System.Guid -ArgumentList $hex; return $g; } function guidtobase64 { param($str); $g = new-object -TypeName System.Guid -ArgumentList $str; $b64 = [System.Convert]::ToBase64String($g.ToByteArray()); return $b64; } </code></pre> <p>An example of the issue I'm having:</p> <pre><code>:) guidtobase64("53E32701-9863-DE11-BD66-0015178A5E3C"); ASfjU2OYEd69ZgAVF4pePA== :) base64toguid("ASfjU2OYEd69ZgAVF4pePA=="); Guid ---- 0127e353-6398-11de-bd66-0015178a5e3c </code></pre> <p>And from the mongo shell:</p> <pre><code>:) mongo MongoDB shell version: 1.6.5 connecting to: test &gt; b = new BinData(3, "ASfjU2OYEd69ZgAVF4pePA=="); BinData(3,"ASfjU2OYEd69ZgAVF4pePA==") &gt; b.hex(); 127e353639811debd66015178a5e3c &gt; </code></pre> <p>So as you can see, the Guid I get back doesn't match what I put in. My function and hex() return the same thing. If you compare the original to the result:</p> <p>53E32701-9863-DE11-BD66-0015178A5E3C<br> 0127e353-6398-11de-bd66-0015178a5e3c</p> <p>You can see that the first 3 sets of hex pairs are reversed, but the last 2 sets are not. This makes me think there is something about Guid.ToString() that I don't understand.</p> <p>Can anyone educate me please?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload