I receive a Guid as a base64 string. By using the Convert.FromBase64String(b64), the conversion is working well, then I try to use the Convert.TryFromBase64String(b64) but in that case, the method return false and my byte array stays empty
var dataString = "kADs1tnSQ+2ehpdtUAK/Jg==";
// output: d6ec0090-d2d9-ed43-9e86-976d5002bf26
Console.WriteLine(new Guid(Convert.FromBase64String(dataString)));
Span<byte> bytes = new();
bool success = Convert.TryFromBase64String(dataString, bytes, out int _);
// output: False, then FormatException
Console.WriteLine(success);
Console.WriteLine(new Guid(success ? bytes : Convert.FromHexString(dataString)));
I guess my problem is with the initialization of the Span, because if I use new byte[16], it is working, but is it safe to write it using a fixed size of 16?
You should provide
byteswith allocated space, e.g.Similar, if you want to use
Span: