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


C# Encoder.Convert方法代码示例

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


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

示例1: EncoderFallbackExceptions_Convert

		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));
		}
开发者ID:killabytenow,项目名称:mono-System.Text.UTF8Encoding-test,代码行数:80,代码来源:SomeTests.cs

示例2: StringToByteArray

 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;
 }
开发者ID:synhershko,项目名称:HSpellCoverageTester,代码行数:9,代码来源:CoverageTester.cs

示例3: WriteWithUInt16LengthPrefix

 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;
 }
开发者ID:lwes,项目名称:lwes-dotnet,代码行数:23,代码来源:LwesSerializer.cs


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