csharpvitamins / CSharpVitamins.ShortGuid

A convenience wrapper for dealing with base64 encoded Guids
MIT License
103 stars 20 forks source link

Little endian decode [feature request] #12

Open kinguru opened 2 years ago

kinguru commented 2 years ago

Dear Dave, could you please add Little Endian guid bytes order support? It's need when Guid is shortened in another application, which default is Little Endian. The solution is easy. In decode method, need to convert Guid byte array to hex string before creating a Guid:

byte[] blob = Convert.FromBase64String(base64);
var hexString = ToHexString(blob);
var guid = new Guid(hexString);

ToHexString could be implemented like:

static string ToHexString(byte[] bytes)
{
    var str1 = BitConverter.ToString(bytes);
    return str1.Replace("-", "");
}

or another more clear algorithm is to swap byte[] bytes, instead of convert to HexString

result[0] = blob[3];
result[1] = blob[2];
result[2] = blob[1];
result[3] = blob[0];
result[4] = blob[5];
result[5] = blob[4];
result[6] = blob[7];
result[7] = blob[6];