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


C# Encoder.GetByteCount方法代码示例

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


在下文中一共展示了Encoder.GetByteCount方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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

示例3: 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

示例4: 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

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