Beispiel #1
0
        private static int DecodeSymbol()
        {
            if (UTF8.byteIndex > UTF8.byteCount)
            {
                throw new UTF8Exception("Invalid byte index");
            }
            if (UTF8.byteIndex == UTF8.byteCount)
            {
                return(-1);
            }
            int num1 = UTF8.byteArray[UTF8.byteIndex] & (int)byte.MaxValue;

            ++UTF8.byteIndex;
            if ((num1 & 128) == 0)
            {
                return(num1);
            }
            if ((num1 & 224) == 192)
            {
                int num2 = UTF8.ReadContinuationByte();
                int num3 = (num1 & 31) << 6 | num2;
                if (num3 >= 128)
                {
                    return(num3);
                }
                throw new UTF8Exception("Invalid continuation byte");
            }
            if ((num1 & 240) == 224)
            {
                int num2 = UTF8.ReadContinuationByte();
                int num3 = UTF8.ReadContinuationByte();
                int num4 = (num1 & 15) << 12 | num2 << 6 | num3;
                if (num4 >= 2048)
                {
                    return(num4);
                }
                throw new UTF8Exception("Invalid continuation byte");
            }
            if ((num1 & 248) == 240)
            {
                int num2 = UTF8.ReadContinuationByte();
                int num3 = UTF8.ReadContinuationByte();
                int num4 = UTF8.ReadContinuationByte();
                int num5 = (num1 & 15) << 18 | num2 << 12 | num3 << 6 | num4;
                if (num5 >= 65536 && num5 <= 1114111)
                {
                    return(num5);
                }
            }
            throw new UTF8Exception("Invalid continuation byte");
        }
Beispiel #2
0
        public static string Decode(string byteString)
        {
            UTF8.byteArray = UTF8.Ucs2Decode(byteString);
            UTF8.byteCount = UTF8.byteArray.Count;
            UTF8.byteIndex = 0;
            List <int> array = new List <int>();
            int        num;

            while ((num = UTF8.DecodeSymbol()) != -1)
            {
                array.Add(num);
            }
            return(UTF8.Ucs2Encode(array));
        }
Beispiel #3
0
        public static string Encode(string str)
        {
            List <int>    intList       = UTF8.Ucs2Decode(str);
            int           count         = intList.Count;
            int           index         = -1;
            StringBuilder stringBuilder = new StringBuilder();

            while (++index < count)
            {
                int codePoint = intList[index];
                stringBuilder.Append(UTF8.EncodeCodePoint(codePoint));
            }
            return(stringBuilder.ToString());
        }