Home > Enterprise >  decode base64 does not give a correct original GUID
decode base64 does not give a correct original GUID

Time:07-02

I have a string GUID I need to decrease its length from 36 to max 30,the problem is I need to parse to to GUID first :

var messageID = Guid.Parse("95ec6174-1d10-4348-a126-faeeb3a026dc");

it gives me a correct GUID but when I convert to base 64 and short it,Im getting a short string out of it but when i decoce that string it give me something weird!

var shortedGuid= System.Convert.ToBase64String(messageID.ToByteArray());

the result from above line is:

dGHslRAdSEOhJvrus6Am3A==

when i decode it I get :

taHC&&

any help will be appreacited

CodePudding user response:

You must be doing something wrong then converting from the base64 string. Use Convert.FromBase64String() to convert from string to byte array, then pass the byte array to the Guid constructor:

Guid originalGuid = Guid.Parse("95ec6174-1d10-4348-a126-faeeb3a026dc");
string base64 = Convert.ToBase64String(originalGuid.ToByteArray());
byte[] decoded = Convert.FromBase64String(base64);

// Pass the byte[] to Guid constructor.
// The result is a Guid = 95ec6174-1d10-4348-a126-faeeb3a026dc
Guid decodedGuid = new Guid(decoded);
  •  Tags:  
  • c#
  • Related