I have binary data in Mongo that was originally a C# Guid:
BinData(3,"QaQPthSsOkat01BUvxApZQ==")
I am extracting d开发者_StackOverflowata with the Ruby driver from 10gen and would like the value as a string. I am using unpack and am getting what I thought was an array of the ascii codes, but on closer inspection there are values above 127.
puts foo["FooID"].unpack("U*")
>> 65
>> 164
>> 15
>> 182
>> 20
>> 172
>> 58
>> 70
>> 173
>> 211
>> 80
>> 84
>> 191
>> 16
>> 41
>> 101
puts foo["FooID"].to_s.unpack("A*")
>> A???:F??PT?)e
How should I be parsing this data? Is there an unpack option that I should be using, or is there a BSON method I need to call?
I am using Ruby 1.9.2 and the latest mongo gem from 10gen. If you'd like any additional details let me know in the comments, thanks.
A GUID/UUID is a 16 byte integer so unpack('C*')
is probably what you want. However, you should compare the results with what C# is getting to make sure you get the byte order right. I'd guess that C# is treating the UUID as a simple list of 16 bytes rather than a big integer so byte order probably isn't an issue; OTOH, best to check and be sure.
Depending on what you need to do with the GUID you might not have to unpack it at all, just treating it as an opaque sequence of 16 bytes may be sufficient.
精彩评论