Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The order of bytes in a GUID are not the same as the order in their <code>ToString()</code> representation on little-endian systems.</p> <p>You should use guid.ToByteArray() rather than using ToString().</p> <p>And, you should use <code>new Guid(byte[] b)</code> to construct it, rather than <code>$str</code>.</p> <p>To express this in pure C#:</p> <pre><code>public string GuidToBase64(Guid guid) { return System.Convert.ToBase64String(guid.ToByteArray()); // Very similar to what you have. } public Guid Base64Toguid(string base64) { var bytes = System.Convert.FromBase64String(base64); return new Guid(bytes); // Not that I'm not building up a string to represent the GUID. } </code></pre> <hr /> <p>Take a look at the <a href="http://en.wikipedia.org/wiki/Globally_unique_identifier#Basic_structure" rel="noreferrer">"Basic Structure" section of the GUID article</a> on Wikipedia for more details.</p> <p>You will see that most of the data is stored in "Native" endianness... which is where the confusion is coming from.</p> <p>To quote:</p> <blockquote> <p>Data4 stores the bytes in the same order as displayed in the GUID text encoding (see below), but the other three fields are reversed on little-endian systems (for example Intel CPUs).</p> </blockquote> <p><hr/></p> <h1>Edit:</h1> <p>Powershell version:</p> <pre><code>function base64toguid { param($str); $b = [System.Convert]::FromBase64String($str); $g = new-object -TypeName System.Guid -ArgumentList (,$b); return $g; } </code></pre> <p>As an additional caveat, you can optionally trim the "==" off of the end of your string, since it is just padding (which may help if you are trying to save space).</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