本文整理汇总了C#中Encoding.GetMaxCharCount方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetMaxCharCount方法的具体用法?C# Encoding.GetMaxCharCount怎么用?C# Encoding.GetMaxCharCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encoding
的用法示例。
在下文中一共展示了Encoding.GetMaxCharCount方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Dump8BitMappings
// Dump the 8-bit byte to Unicode character mappings
// for an encoding.
private static void Dump8BitMappings(Encoding enc)
{
byte[] buf = new byte [1];
char[] chars = new char [enc.GetMaxCharCount(1)];
int value, numChars, ch;
for(value = 0; value < 256; ++value)
{
if((value % 8) == 0)
{
Console.WriteLine();
DumpHex(value, 2);
Console.Write(':');
}
buf[0] = (byte)value;
try
{
numChars = enc.GetChars(buf, 0, 1, chars, 0);
}
catch(ArgumentException)
{
numChars = 0;
}
Console.Write(' ');
if(numChars == 1)
{
ch = chars[0];
if(ch <= 0x20)
{
Console.Write(ctrlNames[ch]);
}
else if(ch < 0x7F)
{
if(ch != '\'')
{
Console.Write('\'');
Console.Write((char)ch);
Console.Write('\'');
Console.Write(' ');
}
else
{
Console.Write("\"'\" ");
}
}
else if(ch == 0x7F)
{
Console.Write("DEL ");
}
else
{
DumpHex(ch, 4);
}
}
else
{
Console.Write("????");
}
if((value % 4) == 3)
{
Console.Write(' ');
}
}
Console.WriteLine();
}
示例2: ValidateConsoleEncoding
public static unsafe void ValidateConsoleEncoding(Encoding encoding)
{
Assert.NotNull(encoding);
// The primary purpose of ConsoleEncoding is to return an empty preamble.
Assert.Equal(Array.Empty<byte>(), encoding.GetPreamble());
// There's not much validation we can do, but we can at least invoke members
// to ensure they don't throw exceptions as they delegate to the underlying
// encoding wrapped by ConsoleEncoding.
Assert.False(string.IsNullOrWhiteSpace(encoding.EncodingName));
Assert.False(string.IsNullOrWhiteSpace(encoding.WebName));
Assert.True(encoding.CodePage >= 0);
bool ignored = encoding.IsSingleByte;
// And we can validate that the encoding is self-consistent by roundtripping
// data between chars and bytes.
string str = "This is the input string.";
char[] strAsChars = str.ToCharArray();
byte[] strAsBytes = encoding.GetBytes(str);
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(str));
Assert.True(encoding.GetMaxByteCount(str.Length) >= strAsBytes.Length);
Assert.Equal(str, encoding.GetString(strAsBytes));
Assert.Equal(str, encoding.GetString(strAsBytes, 0, strAsBytes.Length));
Assert.Equal(str, new string(encoding.GetChars(strAsBytes)));
Assert.Equal(str, new string(encoding.GetChars(strAsBytes, 0, strAsBytes.Length)));
fixed (byte* bytesPtr = strAsBytes)
{
char[] outputArr = new char[encoding.GetMaxCharCount(strAsBytes.Length)];
int len = encoding.GetChars(strAsBytes, 0, strAsBytes.Length, outputArr, 0);
Assert.Equal(str, new string(outputArr, 0, len));
Assert.Equal(len, encoding.GetCharCount(strAsBytes));
Assert.Equal(len, encoding.GetCharCount(strAsBytes, 0, strAsBytes.Length));
fixed (char* charsPtr = outputArr)
{
len = encoding.GetChars(bytesPtr, strAsBytes.Length, charsPtr, outputArr.Length);
Assert.Equal(str, new string(charsPtr, 0, len));
Assert.Equal(len, encoding.GetCharCount(bytesPtr, strAsBytes.Length));
}
Assert.Equal(str, encoding.GetString(bytesPtr, strAsBytes.Length));
}
Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars));
Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars, 0, strAsChars.Length));
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars));
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars, 0, strAsChars.Length));
fixed (char* charsPtr = strAsChars)
{
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(charsPtr, strAsChars.Length));
byte[] outputArr = new byte[encoding.GetMaxByteCount(strAsChars.Length)];
Assert.Equal(strAsBytes.Length, encoding.GetBytes(strAsChars, 0, strAsChars.Length, outputArr, 0));
fixed (byte* bytesPtr = outputArr)
{
Assert.Equal(strAsBytes.Length, encoding.GetBytes(charsPtr, strAsChars.Length, bytesPtr, outputArr.Length));
}
Assert.Equal(strAsBytes.Length, encoding.GetBytes(str, 0, str.Length, outputArr, 0));
}
}
示例3: Decode_Invalid
public static unsafe void Decode_Invalid(Encoding encoding, byte[] bytes, int index, int count)
{
Assert.Equal(DecoderFallback.ExceptionFallback, encoding.DecoderFallback);
char[] chars = new char[encoding.GetMaxCharCount(count)];
if (index == 0 && count == bytes.Length)
{
Assert.Throws<DecoderFallbackException>(() => encoding.GetCharCount(bytes));
Assert.Throws<DecoderFallbackException>(() => encoding.GetChars(bytes));
Assert.Throws<DecoderFallbackException>(() => encoding.GetString(bytes));
}
Assert.Throws<DecoderFallbackException>(() => encoding.GetCharCount(bytes, index, count));
Assert.Throws<DecoderFallbackException>(() => encoding.GetChars(bytes, index, count));
Assert.Throws<DecoderFallbackException>(() => encoding.GetString(bytes, index, count));
Assert.Throws<DecoderFallbackException>(() => encoding.GetChars(bytes, index, count, chars, 0));
fixed (byte* pBytes = bytes)
fixed (char* pChars = chars)
{
byte* pBytesLocal = pBytes;
char* pCharsLocal = pChars;
Assert.Throws<DecoderFallbackException>(() => encoding.GetCharCount(pBytesLocal + index, count));
Assert.Throws<DecoderFallbackException>(() => encoding.GetChars(pBytesLocal + index, count, pCharsLocal, chars.Length));
Assert.Throws<DecoderFallbackException>(() => encoding.GetString(pBytesLocal + index, count));
}
}
示例4: GetMaxCharCount_Invalid
public static void GetMaxCharCount_Invalid(Encoding encoding)
{
Assert.Throws<ArgumentOutOfRangeException>("byteCount", () => encoding.GetMaxCharCount(-1));
// TODO: find a more generic way to find what byteCount is invalid
if (encoding is UTF8Encoding)
{
Assert.Throws<ArgumentOutOfRangeException>("byteCount", () => encoding.GetMaxCharCount(int.MaxValue));
}
// Make sure that GetMaxCharCount respects the MaxCharCount property of DecoderFallback
// However, Utf7Encoding ignores this
if (!(encoding is UTF7Encoding) && !(encoding is UTF32Encoding))
{
Encoding customizedMaxCharCountEncoding = Encoding.GetEncoding(encoding.CodePage, EncoderFallback.ReplacementFallback, new HighMaxCharCountDecoderFallback());
Assert.Throws<ArgumentOutOfRangeException>("byteCount", () => customizedMaxCharCountEncoding.GetMaxCharCount(2));
}
}
示例5: GetMaxCharCount_Invalid
public static void GetMaxCharCount_Invalid(Encoding encoding)
{
Assert.Throws<ArgumentOutOfRangeException>("byteCount", () => encoding.GetMaxCharCount(-1));
}
示例6: PositiveTest
public bool PositiveTest(Encoding enc, int input, int expected, string id)
{
bool result = true;
TestFramework.BeginScenario(id + ": Getting max chars for " + input + " with encoding " + enc.WebName);
try
{
int output = enc.GetMaxCharCount(input);
if (output != expected)
{
result = false;
TestFramework.LogError("001", "Error in " + id + ", unexpected comparison result. Actual count " + output + ", Expected: " + expected);
}
}
catch (Exception exc)
{
result = false;
TestFramework.LogError("002", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
}
return result;
}
示例7: NegativeTest
public bool NegativeTest(Encoding enc, int input, Type excType, string id)
{
bool result = true;
TestFramework.BeginScenario(id + ": Getting max chars for " + input + " with encoding " + enc.WebName);
try
{
int output = enc.GetMaxCharCount(input);
result = false;
TestFramework.LogError("009", "Error in " + id + ", Expected exception not thrown. Actual count " + output + ", Expected exception type: " + excType.ToString());
}
catch (Exception exc)
{
if (exc.GetType() != excType)
{
result = false;
TestFramework.LogError("010", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
}
}
return result;
}