本文整理匯總了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);
}
示例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);
}
}
示例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;
}
示例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;
}
示例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;
}
示例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 ();
}
}
示例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);
}
示例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;
}
示例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);
}