[CSHARP-178] Guid Serialization Bug? Created: 02/Mar/11  Updated: 20/Mar/14  Resolved: 02/Mar/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 0.11

Type: Bug Priority: Major - P3
Reporter: Eric Z. Beard Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7, mongod 1.6.5



 Description   

Maybe I'm doing something wrong here, but the Guid serialization in the C# driver seems very odd.

I save a Guid with a normal windows represenation of "53E32701-9863-DE11-BD66-0015178A5E3C".

I get that Guid back out of mongo from the shell as:

ASfjU2OYEd69ZgAVF4pePA==

If I call hex():

127e353639811debd66015178a5e3c

If I use a little function I wrote in Powershell as follows:

function base64toguid
{
param($str);
$b = [System.Convert]::FromBase64String($str);
$hex = "";
$idx = 0;
foreach ($x in $b)

{ $hex += $x.ToString("x2"); }

$g = new-object -TypeName System.Guid -ArgumentList $hex;
return $g;
}

I get: 0127e353-6398-11de-bd66-0015178a5e3c, which is the same as hex() from the shell.

So to compare them closely:

53E32701-9863-DE11-BD66-0015178A5E3C
0127e353-6398-11de-bd66-0015178a5e3c

The first three sets look like they are reversed as if it's some sort of endian issue, but the last two are not reversed. Since the dashes are totally arbitrary, if it was an endian issue it seems like the whole thing would be reversed.



 Comments   
Comment by Robert Stam [ 02/Mar/11 ]

Even though you've found the explanation for what you were seeing, it still raises an interesting cross platform issue. Reading the Wikipedia article on GUIDs it seems like the string representation is the "official" representation and that the binary representation of that string will vary depending on whether the platform you are on is little endian or big endian.

That poses a problem when exchanging GUIDs between systems in binary form, because the binary form doesn't specify whether it came from a little endian or a big endian system.

So I suppose in the MongoDB world (or the BSON spec) we just have to declare arbitrarily that we will use the binary representation as it would be produced on a little endian system (in other words, like Windows running on Intel hardware). That way we can unambiguously interpret the 16 byte binary array we are storing in and retrieving from MongoDB.

Comment by Eric Z. Beard [ 02/Mar/11 ]

Ok, I figured it out with help from stackoverflow, it's my misconception rather than a bug.

http://stackoverflow.com/questions/5172134/base64-to-guid-to-base64/5172351

It would be cool if there was something like toGuid() in the shell for those of us on windows who need to quickly convert to the regular Guid represenation.

Generated at Wed Feb 07 21:36:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.