当前位置: 首页>>代码示例>>C#>>正文


C# Decoder.Convert方法代码示例

本文整理汇总了C#中System.Text.Decoder.Convert方法的典型用法代码示例。如果您正苦于以下问题:C# Decoder.Convert方法的具体用法?C# Decoder.Convert怎么用?C# Decoder.Convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.Decoder的用法示例。


在下文中一共展示了Decoder.Convert方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DecoderFallbackExceptions_Convert

	// convert bytes to string using a fixed blocksize.
	// If something bad happens, try to recover using the
	// DecoderFallbackException info.
	private void DecoderFallbackExceptions_Convert (
		char [] chars,
		int testno,
		Decoder dec,
		DecoderFallbackExceptionTest t,
		int block_size)
	{
		int charsUsed, bytesUsed;
		bool completed;

		int ce = 0; // current exception
		for (int c = 0; c < t.bytes.Length; ) {
			try {
				int bu = c + block_size > t.bytes.Length
						? t.bytes.Length - c
						: block_size;
				dec.Convert (
					t.bytes, c, bu,
					chars, 0, chars.Length,
					c + bu >= t.bytes.Length,
					out bytesUsed, out charsUsed,
					out completed);
				c += bytesUsed;
			} catch (DecoderFallbackException ex) {
				Assert.IsTrue (
					t.eindex.Length > ce,
					String.Format (
						"test#{0}-2-{1}#{2}: UNEXPECTED FAIL (c={3}, eIndex={4}, eBytesUnknwon={5})",
						testno, block_size, ce, c,
						ex.Index,
						ex.BytesUnknown.Length));
				Assert.IsTrue (
					ex.Index + c == t.eindex[ce],
					String.Format (
						"test#{0}-2-{1}#{2}: Expected at {3} not {4}.",
						testno, block_size, ce,
						t.eindex[ce],
						ex.Index + c));
				Assert.IsTrue (
					ex.BytesUnknown.Length == t.elen[ce],
					String.Format (
						"test#{0}-2-{1}#{2}: Expected BytesUnknown.Length of {3} not {4} @{5}.",
						testno, block_size, ce,
						t.elen[0], ex.BytesUnknown.Length, c));
				for (int i = 0; i < ex.BytesUnknown.Length; i++)
					Assert.IsTrue (
						ex.BytesUnknown[i] == t.bytes[ex.Index + i + c],
						String.Format (
							"test#{0}-2-{1}#{2}: Expected byte {3:X} not {4:X} at {5}.",
							testno, block_size, ce,
							t.bytes[ex.Index + i + c],
							ex.BytesUnknown[i],
							ex.Index + i));
				c += ex.BytesUnknown.Length + ex.Index;
				dec.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));
	}
开发者ID:killabytenow,项目名称:mono-System.Text.UTF8Encoding-test,代码行数:67,代码来源:SomeTests.cs

示例2: PeekChar

                /// <summary>
                /// Convert a single character in the read buffer.
                /// </summary>
                /// <remarks>
                /// This method is designed for the ReadTo() method. One would read byte for byte, without
                /// actually updating the offsets into the buffer, building a string.
                /// <para>Normally, a single character will be read one at a time, from the first available
                /// byte until a complete string is available. The decoder used may maintain state, so the
                /// results given by this function is dependent on the state of the decoder. If a character
                /// is available, its value will be returned and on output the variable <i>bytesRead</i>
                /// will contain the number of bytes that the decoder reports as being used to obtain the
                /// single character. If no character is available in the buffer at the offset provided,
                /// -1 will be returned. The main application should therefore implement timeouts as
                /// necessary.</para>
                /// <para>Note, because of the way that decoders work, it is possible to get no characters
                /// back, but the number of bytes read via <i>bytesRead</i> may be non-zero.</para>
                /// </remarks>
                /// <param name="offset">The offset from the beginning of the read byte buffer to read from</param>
                /// <param name="decoder">The decoder to use to get a single character.</param>
                /// <param name="bytesRead">If a character is read, the number of bytes consumed to generate
                /// this character.</param>
                /// <returns>The character at the position defined by <i>offset</i>.</returns>
                public int PeekChar(int offset, Decoder decoder, out int bytesRead)
                {
                    bytesRead = 0;
                    if (m_Buffers == null) return -1;

                    // We don't lock the ReadBuffer in this case because we're not modifying the
                    // content of the ReadBuffer. We expect it only to grow and also not to
                    // shrink after reading the length, a valid assumption so long as there
                    // is no parallel Read() call during this method.
                    int readlen = m_Buffers.ReadBuffer.Length;
                    if (offset >= readlen) return -1;

                    char[] onechar = new char[1];
                    int cu = 0;
                    bool complete = false;
                    while (cu < 1 && offset < readlen) {
                        int bu;
                        decoder.Convert(m_Buffers.ReadBuffer.Array, m_Buffers.ReadBuffer.ToArrayIndex(offset), 1,
                            onechar, 0, 1, false, out bu, out cu, out complete);
                        bytesRead += bu;
                        offset++;
                    }

                    if (cu == 0) return -1;
                    return onechar[0];
                }
开发者ID:DHMechatronicAG,项目名称:SerialPortStream,代码行数:48,代码来源:SerialPortStream.NativeSerialPort.CommOverlappedIo.cs

示例3: Read

                /// <summary>
                /// Read data from the buffered serial stream into the array provided, using the encoder
                /// given.
                /// </summary>
                /// <remarks>
                /// This function converts data in the buffered stream into the character array provided.
                /// The decoder provided is used for this conversion. The decoder may contain state from
                /// a previous encoding/decoding, so it is up to the main application to decide if this
                /// is acceptable or not.
                /// <para>The number of characters converted are returned, not the number of bytes that
                /// were consumed in the incoming buffer.</para>
                /// <para>You may receive notification that data is available to read with the
                /// WaitForReadEvent(), but still have no data returned. This could be the case in
                /// particular if a multi-byte character is in the pipeline, but it hasn't been
                /// completely transmitted. For example, the Euro symbol is three bytes in the UTF8
                /// encoding scheme. If only one byte arrives, there is insufficient bytes to generate
                /// a single character. You should loop until a timeout occurs.</para>
                /// </remarks>
                /// <param name="buffer">Buffer convert data into.</param>
                /// <param name="offset">Offset into buffer.</param>
                /// <param name="count">Number of characters to write.</param>
                /// <param name="decoder">The decoder to use.</param>
                /// <param name="bytesUsed">Number of bytes consumed by the internal read buffer.</param>
                /// <returns>Number of characters copied into the buffer.</returns>
                public int Read(char[] buffer, int offset, int count, Decoder decoder, out int bytesUsed)
                {
                    if (m_Buffers == null) {
                        bytesUsed = 0;
                        return -1;
                    }

                    int cu;
                    bool completed;

                    lock (m_ReadLock) {
                        decoder.Convert(m_Buffers.ReadBuffer, buffer, offset, count, false, out bytesUsed, out cu, out completed);
                    }
                    return cu;
                }
开发者ID:DHMechatronicAG,项目名称:SerialPortStream,代码行数:39,代码来源:SerialPortStream.NativeSerialPort.CommOverlappedIo.cs

示例4: ReadChar

                /// <summary>
                /// Read data from the buffered serial stream, using the encoder given.
                /// </summary>
                /// <remarks>
                /// This function will convert to a single character using the current decoder. The
                /// decoder may maintain state from a previous Read(char[], ...) operation.
                /// <para>You may receive notification that data is available to read with the
                /// WaitForReadEvent(), but still have no data returned. This could be the case in
                /// particular if a multi-byte character is in the pipeline, but it hasn't been
                /// completely transmitted. For example, the Euro symbol is three bytes in the UTF8
                /// encoding scheme. If only one byte arrives, there is insufficient bytes to generate
                /// a single character. You should loop until a timeout occurs.</para>
                /// </remarks>
                /// <param name="decoder">The decoder to use.</param>
                /// <returns>The character interpreted, or -1 if no data available.</returns>
                public int ReadChar(Decoder decoder)
                {
                    if (m_Buffers == null) return -1;

                    int bu;
                    int cu;
                    bool completed;
                    char[] c = new char[1];

                    lock (m_ReadLock) {
                        decoder.Convert(m_Buffers.ReadBuffer, c, 0, 1, false, out bu, out cu, out completed);
                    }
                    if (cu != 1) return -1;
                    return c[0];
                }
开发者ID:DHMechatronicAG,项目名称:SerialPortStream,代码行数:30,代码来源:SerialPortStream.NativeSerialPort.CommOverlappedIo.cs

示例5: ReadStringWithUInt16LengthPrefix

 private static string ReadStringWithUInt16LengthPrefix(byte[] buffer, ref int offset, Decoder coder)
 {
     #if DEBUG
     if (buffer == null) throw new ArgumentNullException("buffer");
     if (offset < 0 || offset > buffer.Length - 1) throw new ArgumentOutOfRangeException("offset");
     if (coder == null) throw new ArgumentNullException("coder");
     #endif
     int ofs = offset;
     int count = ReadUInt16(buffer, ref ofs);
     if (ofs + count > buffer.Length)
         throw new BadLwesDataException(String.Concat("Cannot deserialize incoming string at offset ", ofs));
     char[] result = new char[count];
     int bytesUsed;
     int charsUsed;
     bool completed;
     coder.Convert(buffer, ofs, count, result, 0, count, true
         , out bytesUsed
         , out charsUsed
         , out completed);
     if (!completed)
         throw new ArgumentException(String.Format(
             Properties.Resources.Error_BufferOutOfSpace), "buffer");
     offset = ofs + bytesUsed;
     return new String(result, 0, charsUsed);
 }
开发者ID:lwes,项目名称:lwes-dotnet,代码行数:25,代码来源:LwesSerializer.cs


注:本文中的System.Text.Decoder.Convert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。