Disco.Services.DocumentUniqueIdentifierExtensions.TryBinaryAlphaEncode C# (CSharp) Method

TryBinaryAlphaEncode() public static method

public static TryBinaryAlphaEncode ( string Data, byte &Result ) : bool
Data string
Result byte
return bool
        public static bool TryBinaryAlphaEncode(string Data, out byte[] Result)
        {
            // byte[0] = XXYY YYYY
            //              X = 01 = alpha encoded
            //              Y = data length <= 63
            // byte[1.] = AAAA ABBB
            // byte[2.] = BBCD DDDD
            //              A = first character index
            //              B = second character index
            //              C = not used
            //              D = third character index

            if (Data.Length == 0)
            {
                // Zero-length Alpha Encode (1 byte)
                Result = new byte[] { 0x40 };
                return true;
            }

            if (Data.Length > 0 && Data.Length <= 63)
            {
                Data = Data.ToUpperInvariant();
                var position = 1;
                var requiredBytes = ((Data.Length / 3) * 2);
                switch (Data.Length % 3)
                {
                    case 2:
                        requiredBytes += 3;
                        break;
                    case 1:
                        requiredBytes += 2;
                        break;
                    default:
                        requiredBytes++;
                        break;
                }

                Result = new byte[requiredBytes];
                Result[0] = (byte)(0x40 | Data.Length);
                for (int i = 0; i < Data.Length; i++)
                {
                    var charIndex = AlphaEncodeMap.IndexOf(Data[i]);
                    if (charIndex == -1)
                    {
                        Result = null;
                        return false;
                    }
                    switch (i % 3)
                    {
                        case 0:
                            Result[position] = (byte)(charIndex << 3);
                            break;
                        case 1:
                            Result[position] = (byte)(Result[position] | (charIndex >> 2));
                            Result[++position] = (byte)(charIndex << 6);
                            break;
                        case 2:
                            Result[position] = (byte)(Result[position] | charIndex);
                            position++;
                            break;
                    }
                }

                return true;
            }
            Result = null;
            return false;
        }