1

i have a BinData in MongoDB and i would convert in Guid in c#. For example this is my Guid in MongoDB document: BinData(3, 'E7hI3meCkEC5C/KU8w7BRQ==') I would convert in c# Guid or c# string.

Thanks

I try to Convert with ConvertFromBase64String but i have an error

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • Show the code. To attract high value answers: check your question if it complys to the suggestions in [ask] and improve it accordingly. – Fildor Feb 21 '23 at 09:22

1 Answers1

0

Without more details, check this code:

        var guid = new Guid();
        var binary = new BsonBinaryData(guid, GuidRepresentation.Standard);
        var result = binary.ToGuid(GuidRepresentation.Standard);
        (guid == result).Should().BeTrue();

or:

        var bytes = Convert.FromBase64String("E7hI3meCkEC5C/KU8w7BRQ==");
        var binaryData = new BsonBinaryData(bytes, BsonBinarySubType.UuidLegacy);
        var result = binaryData.ToGuid(GuidRepresentation.CSharpLegacy);
        var guid = new Guid(bytes);
        (guid == result).Should().BeTrue();
dododo
  • 3,872
  • 1
  • 14
  • 37