本文整理汇总了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);
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}