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

BinaryUTF8Encode() public static method

public static BinaryUTF8Encode ( string Data ) : byte[]
Data string
return byte[]
        public static byte[] BinaryUTF8Encode(string Data)
        {
            // byte[0] = XXYY YYYY
            //              X = 11 = UTF8 encoded
            //              Y = data length <= 63
            // byte[.] = AAAA AAAA
            //              A = UTF8 encoded string

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

            if (Data.Length <= 63)
            {
                var utf8Bytes = Encoding.UTF8.GetBytes(Data);
                if (utf8Bytes.Length <= 63)
                {
                    var result = new byte[1 + utf8Bytes.Length];
                    result[0] = (byte)(0xC0 | utf8Bytes.Length);
                    utf8Bytes.CopyTo(result, 1);
                    return result;
                }
            }

            throw new ArgumentException("Unable to encode the data. The input data is to long.");
        }