Example #1
0
        public static ArrayByte EncodeUTF8(string str, int orderType)
        {
            int      offset = 0;
            int      c1     = 0;
            int      c2     = 0;
            IntArray buffer = new IntArray(GetUTF8ByteLength(str));

            for (int i = 0; i < str.Length(); i++)
            {
                c1 = str.CharAt(i);
                if (c1 < 128)
                {
                    buffer.Set(offset++, c1);
                }
                else if (c1 < 2048)
                {
                    buffer.Set(offset++, c1 >> 6 | 192);
                    buffer.Set(offset++, c1 & 63 | 128);
                }
                else if ((c1 & 0xFC00) == 0xD800 && ((c2 = str.CharAt(i + 1)) & 0xFC00) == 0xDC00)
                {
                    c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
                    ++i;
                    buffer.Set(offset++, c1 >> 18 | 240);
                    buffer.Set(offset++, c1 >> 12 & 63 | 128);
                    buffer.Set(offset++, c1 >> 6 & 63 | 128);
                    buffer.Set(offset++, c1 & 63 | 128);
                }
                else
                {
                    buffer.Set(offset++, c1 >> 12 | 224);
                    buffer.Set(offset++, c1 >> 6 & 63 | 128);
                    buffer.Set(offset++, c1 & 63 | 128);
                }
            }
            ArrayByte bytes = new ArrayByte(offset);

            bytes.SetByteOrder(orderType);
            for (int i = 0; i < offset; i++)
            {
                bytes.WriteByte(buffer.Get(i));
            }
            return(bytes);
        }
Example #2
0
 public override void Write(int b)
 {
     _buffer.WriteByte(b);
 }