當前位置: 首頁>>代碼示例>>C#>>正文


C# Encoder.GetBytes方法代碼示例

本文整理匯總了C#中System.Text.Encoder.GetBytes方法的典型用法代碼示例。如果您正苦於以下問題:C# Encoder.GetBytes方法的具體用法?C# Encoder.GetBytes怎麽用?C# Encoder.GetBytes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Text.Encoder的用法示例。


在下文中一共展示了Encoder.GetBytes方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Hash

 public static int Hash(string data, Encoder enc)
 {
     var arr = data.ToCharArray();
     int count = enc.GetByteCount(arr, 0, arr.Length, false);
     var bytes = new byte[count];
     enc.GetBytes(arr, 0, arr.Length, bytes, 0, false);
     return Hash(bytes);
 }
開發者ID:r-bel,項目名稱:glorg2,代碼行數:8,代碼來源:CyclicRedundancy.cs

示例2: FlushAsyncInternal

        // We pass in private instance fields of this MarshalByRefObject-derived type as local params
        // to ensure performant access inside the state machine that corresponds this async method.
        private static async Task FlushAsyncInternal(StreamWriter _this, bool flushStream, bool flushEncoder,
                                                     char[] charBuffer, int charPos, bool haveWrittenPreamble,
                                                     Encoding encoding, Encoder encoder, Byte[] byteBuffer, Stream stream)
        {
            if (!haveWrittenPreamble)
            {
                _this.HaveWrittenPreamble_Prop = true;
                byte[] preamble = encoding.GetPreamble();
                if (preamble.Length > 0)
                {
                    await stream.WriteAsync(preamble, 0, preamble.Length).ConfigureAwait(false);
                }
            }

            int count = encoder.GetBytes(charBuffer, 0, charPos, byteBuffer, 0, flushEncoder);
            if (count > 0)
            {
                await stream.WriteAsync(byteBuffer, 0, count).ConfigureAwait(false);
            }

            // By definition, calling Flush should flush the stream, but this is
            // only necessary if we passed in true for flushStream.  The Web
            // Services guys have some perf tests where flushing needlessly hurts.
            if (flushStream)
            {
                await stream.FlushAsync().ConfigureAwait(false);
            }
        }
開發者ID:shiftkey-tester,項目名稱:corefx,代碼行數:30,代碼來源:StreamWriter.cs

示例3: CreateFirstSubstData

        // WOS 1926509: ASP.NET:  WriteSubstitution in integrated mode needs to support callbacks that return String.Empty
        private unsafe void CreateFirstSubstData(String s, IIS7WorkerRequest iis7WorkerRequest, Encoder encoder) {
            Debug.Assert(s != null, "s != null");

            IntPtr pbBuffer;
            int numBytes = 0;
            int cch = s.Length;
            if (cch > 0) {
                fixed (char * pch = s) {
                    int cbBuffer = encoder.GetByteCount(pch, cch, true /*flush*/);
                    pbBuffer = iis7WorkerRequest.AllocateRequestMemory(cbBuffer);
                    if (pbBuffer != IntPtr.Zero) {
                        numBytes = encoder.GetBytes(pch, cch, (byte*)pbBuffer, cbBuffer, true /*flush*/);
                    }
                }
            }
            else {
                // deal with empty string
                pbBuffer = iis7WorkerRequest.AllocateRequestMemory(1);
            }

            if (pbBuffer == IntPtr.Zero) {
                throw new OutOfMemoryException();
            }
            _firstSubstData = pbBuffer;
            _firstSubstDataSize = numBytes;
        }
開發者ID:JokerMisfits,項目名稱:linux-packaging-mono,代碼行數:27,代碼來源:HttpWriter.cs

示例4: UnsafeAppendEncodedChars

        private unsafe static int UnsafeAppendEncodedChars(char[] src, int srcOffset, int srcSize, IntPtr dest, int destOffset, int destSize, Encoder encoder, bool flushEncoder) {
            int numBytes = 0;

            byte* destBytes = ((byte*)dest) + destOffset;

            fixed (char* charSrc = src) {
                numBytes = encoder.GetBytes(charSrc+srcOffset, srcSize, destBytes, destSize, flushEncoder);
            }

            return numBytes;
        }
開發者ID:JokerMisfits,項目名稱:linux-packaging-mono,代碼行數:11,代碼來源:HttpWriter.cs

示例5: AppendEncodedChars

 internal override void AppendEncodedChars(char[] data, int offset, int size, Encoder encoder, bool flushEncoder) {
     int byteSize = encoder.GetBytes(data, offset, size, _data, _size-_free, flushEncoder);
     _free -= byteSize;
 }
開發者ID:JokerMisfits,項目名稱:linux-packaging-mono,代碼行數:4,代碼來源:HttpWriter.cs

示例6: EncoderFallbackExceptions_GetBytes

		// try to encode some bytes at once with GetBytes
		private void EncoderFallbackExceptions_GetBytes (
			byte [] bytes,
			int testno,
			Encoder enc,
			EncoderFallbackExceptionTest t)
		{
			try {
				enc.GetBytes (
					t.str.ToCharArray (), 0, t.str.Length,
					bytes, 0, true);
				Assert.IsTrue (
					t.eindex.Length == 0,
					String.Format (
						"test#{0}-1: UNEXPECTED SUCCESS",
						testno));
			} catch(EncoderFallbackException ex) {
				Assert.IsTrue (
					t.eindex.Length > 0,
					String.Format (
						"test#{0}-1: UNEXPECTED FAIL",
						testno));
				Assert.IsTrue (
					ex.Index == t.eindex[0],
					String.Format (
						"test#{0}-1: Expected exception at {1} not {2}.",
						testno, t.eindex[0], ex.Index));
				Assert.IsTrue (
					!ex.IsUnknownSurrogate (),
					String.Format (
						"test#{0}-1: Expected false not {1} in IsUnknownSurrogate().",
						testno,
						ex.IsUnknownSurrogate ()));
				// NOTE: I know that in the previous check we
				// have asserted that ex.IsUnknownSurrogate()
				// is always false, but this does not mean that
				// we don't have to take in consideration its
				// real value for the next check.
				if (ex.IsUnknownSurrogate ())
					Assert.IsTrue (
						ex.CharUnknownHigh == t.str[ex.Index]
						&& ex.CharUnknownLow == t.str[ex.Index + 1],
						String.Format (
							"test#{0}-1: expected ({1:X}, {2:X}) not ({3:X}, {4:X}).",
							testno,
							t.str[ex.Index],
							t.str[ex.Index + 1],
							ex.CharUnknownHigh,
							ex.CharUnknownLow));
				else
					Assert.IsTrue (
						ex.CharUnknown == t.str[ex.Index],
						String.Format (
							"test#{0}-1: expected ({1:X}) not ({2:X}).",
							testno,
							t.str[ex.Index],
							ex.CharUnknown));
				enc.Reset ();
			}
		}
開發者ID:killabytenow,項目名稱:mono-System.Text.UTF8Encoding-test,代碼行數:60,代碼來源:SomeTests.cs

示例7: GetNextValue

        static bool GetNextValue(string charset, Encoder encoder, HexEncoder hex, char[] chars, ref int index,
		                          ref byte[] bytes, ref byte[] encoded, int maxLength, out string value)
        {
            int length = chars.Length - index;

            if (length < maxLength) {
                switch (GetEncodeMethod (chars, index, length)) {
                case EncodeMethod.Quote:
                    value = MimeUtils.Quote (new string (chars, index, length));
                    index += length;
                    return false;
                case EncodeMethod.None:
                    value = new string (chars, index, length);
                    index += length;
                    return false;
                }
            }

            length = Math.Min (maxLength, length);
            int ratio, count, n;

            do {
                count = encoder.GetByteCount (chars, index, length, true);
                if (count > maxLength && length > 1) {
                    ratio = (int) Math.Round ((double) count / (double) length);
                    length -= Math.Max ((count - maxLength) / ratio, 1);
                    continue;
                }

                if (bytes.Length < count)
                    Array.Resize<byte> (ref bytes, count);

                count = encoder.GetBytes (chars, index, length, bytes, 0, true);

                // Note: the first chunk needs to be encoded in order to declare the charset
                if (index > 0 || charset == "us-ascii") {
                    var method = GetEncodeMethod (bytes, count);

                    if (method == EncodeMethod.Quote) {
                        value = MimeUtils.Quote (Encoding.ASCII.GetString (bytes, 0, count));
                        index += length;
                        return false;
                    }

                    if (method == EncodeMethod.None) {
                        value = Encoding.ASCII.GetString (bytes, 0, count);
                        index += length;
                        return false;
                    }
                }

                n = hex.EstimateOutputLength (count);
                if (encoded.Length < n)
                    Array.Resize<byte> (ref encoded, n);

                // only the first value gets a charset declaration
                int charsetLength = index == 0 ? charset.Length + 2 : 0;

                n = hex.Encode (bytes, 0, count, encoded);
                if (n > 3 && (charsetLength + n) > maxLength) {
                    int x = 0;

                    for (int i = n - 1; i >= 0 && charsetLength + i >= maxLength; i--) {
                        if (encoded[i] == (byte) '%')
                            x--;
                        else
                            x++;
                    }

                    ratio = (int) Math.Round ((double) count / (double) length);
                    length -= Math.Max (x / ratio, 1);
                    continue;
                }

                if (index == 0)
                    value = charset + "''" + Encoding.ASCII.GetString (encoded, 0, n);
                else
                    value = Encoding.ASCII.GetString (encoded, 0, n);
                index += length;
                return true;
            } while (true);
        }
開發者ID:vdaron,項目名稱:MimeKit,代碼行數:82,代碼來源:Parameter.cs

示例8: ObjectToBytes

 internal override byte[] ObjectToBytes(object o, Encoder encoder)
 {
     byte[] res = new byte[empty.Length];
     Array.Copy(empty, res, empty.Length);
     if (o == null)
     {
         if (!Nullable)
             throw new Exception("Null value");
         return res;
     }
     if (!(o is string))
         throw new Exception("Wrong type: string expected");
     string str = (string)o;
     char[] charArray = str.ToCharArray();
     if (!truncate && charArray.Length > res.Length)
         throw new Exception("String too long. " + res.Length + " symbols expexted, " + charArray.Length + " symbols received: " + str);
     encoder.GetBytes(charArray, 0, Math.Min(charArray.Length, res.Length), res, 0, true);
     return res;
 }
開發者ID:LSTANCZYK,項目名稱:devexpress_xaf_aurum,代碼行數:19,代碼來源:Dbf.cs

示例9: SetEncodedName

 /// <summary>
 /// Закодировать название
 /// </summary>
 /// <param name="encoder">Кодировщик</param>
 public void SetEncodedName(Encoder encoder)
 {
     char[] charArray = Name.ToCharArray();
     desc.fieldName = new byte[11];
     encoder.GetBytes(charArray, 0, charArray.Length, desc.fieldName, 0, true);
 }
開發者ID:LSTANCZYK,項目名稱:devexpress_xaf_aurum,代碼行數:10,代碼來源:Dbf.cs


注:本文中的System.Text.Encoder.GetBytes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。