Convert() приватный Метод

private Convert ( char chars, int charCount, byte bytes, int byteCount, bool flush, int &charsUsed, int &bytesUsed, bool &completed ) : void
chars char
charCount int
bytes byte
byteCount int
flush bool
charsUsed int
bytesUsed int
completed bool
Результат void
Пример #1
0
        public static string UrlEncode(string str, string encode)
        {
            int factor = 0;

            if (encode == "UTF-8")
            {
                factor = 3;
            }
            if (encode == "GB2312")
            {
                factor = 2;
            }
            //不需要编码的字符

            string okChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.";

            System.Text.Encoder encoder = System.Text.Encoding.GetEncoding(encode).GetEncoder();
            char[] c1 = str.ToCharArray();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //一个字符一个字符的编码

            for (int i = 0; i < c1.Length; i++)
            {
                //不需要编码

                if (okChar.IndexOf(c1[i]) > -1)
                {
                    sb.Append(c1[i]);
                }
                else
                {
                    byte[] c2 = new byte[factor];
                    int    charUsed, byteUsed; bool completed;

                    encoder.Convert(c1, i, 1, c2, 0, factor, true, out charUsed, out byteUsed, out completed);

                    foreach (byte b in c2)
                    {
                        if (b != 0)
                        {
                            sb.AppendFormat("%{0:X}", b);
                        }
                    }
                }
            }
            return(sb.ToString().Trim());
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (count < _minimumTargetBufferCount)
            {
                // The buffer must be able to hold at least one character from the
                // SourceText stream.  Returning 0 for that case isn't correct because
                // that indicates end of stream vs. insufficient buffer.
                throw new ArgumentException($"{nameof(count)} must be greater than or equal to {_minimumTargetBufferCount}", nameof(count));
            }

            int originalCount = count;

            if (!_preambleWritten)
            {
                int bytesWritten = WritePreamble(buffer, offset, count);
                offset += bytesWritten;
                count  -= bytesWritten;
            }

            while (count >= _minimumTargetBufferCount && _position < _source.Length)
            {
                if (_bufferUnreadChars == 0)
                {
                    FillBuffer();
                }

                bool ignored;
                _encoder.Convert(_charBuffer,
                                 _bufferOffset,
                                 _bufferUnreadChars,
                                 buffer,
                                 offset,
                                 count,
                                 false,
                                 out int charsUsed,
                                 out int bytesUsed,
                                 out ignored);
                _position          += charsUsed;
                _bufferOffset      += charsUsed;
                _bufferUnreadChars -= charsUsed;
                offset             += bytesUsed;
                count -= bytesUsed;
            }

            // Return value is the number of bytes read
            return(originalCount - count);
        }
Пример #3
0
        public static string Utf8Encode(this string url)
        {
            int factor = 3;

            //不需要编码的字符
            System.Text.Encoder encoder = System.Text.Encoding.GetEncoding("UTF-8").GetEncoder();
            char[] c1 = url.ToCharArray();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //一个字符一个字符的编码
            for (int i = 0; i < c1.Length; i++)
            {
                //不需要编码
                if (c1[i] < 128)
                {
                    sb.Append(c1[i]);
                }
                else
                {
                    byte[] c2 = new byte[factor];
                    int    charUsed, byteUsed; bool completed;

                    encoder.Convert(c1, i, 1, c2, 0, factor, true, out charUsed, out byteUsed, out completed);

                    foreach (byte b in c2)
                    {
                        if (b != 0)
                        {
                            sb.AppendFormat("%{0:X}", b);
                        }
                    }
                }
            }
            return(sb.ToString().Trim());

            //            return HttpUtility.UrlEncode(url, System.Text.Encoding.UTF8);
        }
 protected static byte[] StringToByteArray(string str, Encoder enc)
 {
     var ca = str.ToCharArray();
     var ret = new byte[enc.GetByteCount(ca, 0, ca.Length, false)];
     int charsUsed, bytesUsed;
     bool completed;
     enc.Convert(ca, 0, ca.Length, ret, 0, ret.Length, true, out charsUsed, out bytesUsed, out completed);
     return ret;
 }
		private void EncoderFallbackExceptions_Convert (
			byte [] bytes,
			int testno,
			Encoder enc,
			EncoderFallbackExceptionTest t,
			int block_size)
		{
			int charsUsed, bytesUsed;
			bool completed;

			int ce = 0; // current exception

			for (int c = 0; c < t.str.Length; ) {
				//Console.WriteLine ("test#{0}-2-{1}: c={2}", testno, block_size, c);
				try {
					int bu = c + block_size > t.str.Length
							? t.str.Length - c
							: block_size;
					enc.Convert (
						t.str.ToCharArray (), c, bu,
						bytes, 0, bytes.Length,
						c + bu >= t.str.Length,
						out charsUsed, out bytesUsed,
						out completed);
					c += charsUsed;
				} catch (EncoderFallbackException ex) {
					//Console.WriteLine (
					//	"test#{0}-2-{1}#{2}: Exception (Index={3}, UnknownSurrogate={4})",
					//	testno, block_size, ce,
					//	ex.Index, ex.IsUnknownSurrogate ());
					Assert.IsTrue (
						ce < t.eindex.Length,
						String.Format (
							"test#{0}-2-{1}#{2}: UNEXPECTED EXCEPTION (Index={3}, UnknownSurrogate={4})",
							testno, block_size, ce,
							ex.Index,
							ex.IsUnknownSurrogate ()));
					Assert.IsTrue (
						ex.Index + c == t.eindex[ce],
						String.Format (
							"test#{0}-2-{1}#{2}: Expected exception at {3} not {4}.",
							testno, block_size, ce,
							t.eindex[ce],
							ex.Index + c));
					Assert.IsTrue (
						!ex.IsUnknownSurrogate (),
						String.Format (
							"test#{0}-2-{1}#{2}: Expected false not {3} in IsUnknownSurrogate().",
							testno, block_size, ce,
							ex.IsUnknownSurrogate ()));
					if (ex.IsUnknownSurrogate ()) {
						Assert.IsTrue (
							ex.CharUnknownHigh == t.str[ex.Index + c]
							&& ex.CharUnknownLow == t.str[ex.Index + c + 1],
							String.Format (
								"test#{0}-2-{1}#{2}: expected ({3:X}, {4:X}) not ({5:X}, {6:X}).",
								testno, block_size, ce,
								t.str[ex.Index + c], t.str[ex.Index + c + 1],
								ex.CharUnknownHigh, ex.CharUnknownLow));
						c += ex.Index + 2;
					} else {
						Assert.IsTrue (
							ex.CharUnknown == t.str[ex.Index + c],
							String.Format (
								"test#{0}-2-{1}#{2}: expected ({3:X}) not ({4:X}).",
								testno, block_size, ce,
								t.str[ex.Index + c],
								ex.CharUnknown));
						c += ex.Index + 1;
					}
					enc.Reset ();
					ce++;
				}
			}
			Assert.IsTrue (
				ce == t.eindex.Length,
				String.Format (
					"test#{0}-2-{1}: UNEXPECTED SUCCESS (expected {2} exceptions, but happened {3})",
					testno, block_size, t.eindex.Length, ce));
		}
 /// <summary>
 /// Encodes a block of input characters to bytes
 /// </summary>
 /// <param name="encoder">The encoder performing the encoding operation</param>
 /// <param name="input">The input characters</param>
 /// <param name="output">The span that receives the output</param>
 /// <param name="flush">True to flush the encoder, otherwise False</param>
 /// <param name="charsRead">Receives the number of characters read</param>
 /// <param name="bytesWritten">Receives the number of bytes written</param>
 /// <param name="isCompleted">Receives whether all output bytes have been written</param>
 public static unsafe void Convert(this Encoder encoder, ReadOnlySpan <char> input, Span <byte> output, bool flush, out int charsRead, out int bytesWritten, out bool isCompleted)
 {
     fixed(char *pInput = &MemoryMarshal.GetReference(input))
     fixed(byte *pOutput = &MemoryMarshal.GetReference(output))
     encoder.Convert(pInput, input.Length, pOutput, output.Length, flush, out charsRead, out bytesWritten, out isCompleted);
 }
 public static void Convert(Encoder encoder, ReadOnlySpan <char> input, Span <byte> output, bool flush, out int charsRead, out int bytesWritten, out bool isCompleted) => encoder.Convert(input, output, flush, out charsRead, out bytesWritten, out isCompleted);
Пример #8
0
 private static int WriteWithUInt16LengthPrefix(byte[] buffer, ref int offset, Char[] value, Encoder coder)
 {
     #if DEBUG
     if (buffer == null) throw new ArgumentNullException("buffer");
     if (value == null) throw new ArgumentNullException("value");
     if (coder == null) throw new ArgumentNullException("coder");
     #endif
     int ofs = offset;
     int count = coder.GetByteCount(value, 0, value.Length, true);
     int bytesUsed;
     int charsUsed;
     bool completed;
     Write(buffer, ref ofs, (UInt16)count);
     coder.Convert(value, 0, value.Length, buffer, ofs, buffer.Length - ofs, true
         , out charsUsed
         , out bytesUsed
         , out completed);
     if (!completed)
         throw new ArgumentException(String.Format(
             Properties.Resources.Error_BufferOutOfSpace), "buffer");
     offset = ofs + bytesUsed;
     return bytesUsed + 2;
 }