本文整理汇总了C#中Encoding.GetMaxByteCount方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetMaxByteCount方法的具体用法?C# Encoding.GetMaxByteCount怎么用?C# Encoding.GetMaxByteCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encoding
的用法示例。
在下文中一共展示了Encoding.GetMaxByteCount方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestEncoding
private void TestEncoding(Encoding enc, int byteCount, int maxByteCount, byte[] bytes)
{
Assert.Equal(byteCount, enc.GetByteCount(s_myChars));
Assert.Equal(maxByteCount, enc.GetMaxByteCount(s_myChars.Length));
Assert.Equal(enc.GetBytes(s_myChars), bytes);
Assert.Equal(enc.GetCharCount(bytes), s_myChars.Length);
Assert.Equal(enc.GetChars(bytes), s_myChars);
Assert.Equal(enc.GetString(bytes, 0, bytes.Length), s_myString);
Assert.NotEqual(0, enc.GetHashCode());
}
示例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: PositiveTest
public bool PositiveTest(Encoding enc, int input, int expected, string id)
{
bool result = true;
TestFramework.BeginScenario(id + ": Getting max bytes for " + input + " with encoding " + enc.WebName);
try
{
int output = enc.GetMaxByteCount(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;
}
示例4: NegativeTest
public bool NegativeTest(Encoding enc, int input, Type excType, string id)
{
bool result = true;
TestFramework.BeginScenario(id + ": Getting max bytes for " + input + " with encoding " + enc.WebName);
try
{
int output = enc.GetMaxByteCount(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;
}
示例5: DumpEncoding
// Dump the Unicode character mappings for an encoding.
private static void DumpEncoding(Encoding enc, bool fromCodePage)
{
byte[] buf = new byte [enc.GetMaxByteCount(1)];
char[] chars = new char [1];
ushort[] usage = new ushort [65535];
int value, numBytes, index, codeValue;
Console.WriteLine("# Code page {0} - {1}",
enc.CodePage, enc.EncodingName);
Console.WriteLine();
for(value = 0; value < 65536; ++value)
{
chars[0] = (char)value;
try
{
numBytes = enc.GetBytes(chars, 0, 1, buf, 0);
}
catch(ArgumentException)
{
numBytes = 0;
}
catch(NotSupportedException)
{
numBytes = 0;
}
if(numBytes > 0 &&
(numBytes != 1 || buf[0] != (byte)'?' || value == (int)'?'))
{
if(fromCodePage)
{
Console.Write("0x");
for(index = 0; index < numBytes; ++index)
{
DumpHex(buf[index], -2);
}
Console.Write(" 0x");
DumpHex(value, -4);
Console.WriteLine();
}
else
{
Console.Write("<U");
DumpHex(value, -4);
Console.Write("> \\x");
codeValue = 0;
for(index = 0; index < numBytes; ++index)
{
codeValue = (codeValue << 8) + buf[index];
DumpHex(buf[index], -2);
}
if(codeValue < 0x10000)
{
Console.WriteLine(" |{0}", usage[codeValue]);
++(usage[codeValue]);
}
else
{
Console.WriteLine();
}
}
}
}
}
示例6: UrlEncode
public static string UrlEncode (string s, Encoding Enc)
{
if (s == null)
return null;
if (s == String.Empty)
return String.Empty;
bool needEncode = false;
int len = s.Length;
for (int i = 0; i < len; i++) {
char c = s [i];
if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) {
if (HttpEncoderFromMono.NotEncoded (c))
continue;
needEncode = true;
break;
}
}
if (!needEncode)
return s;
// avoided GetByteCount call
byte [] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
int realLen = Enc.GetBytes (s, 0, s.Length, bytes, 0);
return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, realLen));
}
示例7: Encode_Invalid
public static unsafe void Encode_Invalid(Encoding encoding, string chars, int index, int count)
{
Assert.Equal(EncoderFallback.ExceptionFallback, encoding.EncoderFallback);
char[] charsArray = chars.ToCharArray();
byte[] bytes = new byte[encoding.GetMaxByteCount(count)];
if (index == 0 && count == chars.Length)
{
Assert.Throws<EncoderFallbackException>(() => encoding.GetByteCount(chars));
Assert.Throws<EncoderFallbackException>(() => encoding.GetByteCount(charsArray));
Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(chars));
Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(charsArray));
}
Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(charsArray, index, count));
Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(chars, index, count, bytes, 0));
Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(charsArray, index, count, bytes, 0));
fixed (char* pChars = chars)
fixed (byte* pBytes = bytes)
{
char* pCharsLocal = pChars;
byte* pBytesLocal = pBytes;
Assert.Throws<EncoderFallbackException>(() => encoding.GetByteCount(pCharsLocal + index, count));
Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(pCharsLocal + index, count, pBytesLocal, bytes.Length));
}
}
示例8: GetMaxByteCount_Invalid
public static void GetMaxByteCount_Invalid(Encoding encoding)
{
Assert.Throws<ArgumentOutOfRangeException>("charCount", () => encoding.GetMaxByteCount(-1));
if (!(encoding is ASCIIEncoding))
{
Assert.Throws<ArgumentOutOfRangeException>("charCount", () => encoding.GetMaxByteCount(int.MaxValue / 2));
}
Assert.Throws<ArgumentOutOfRangeException>("charCount", () => encoding.GetMaxByteCount(int.MaxValue));
// Make sure that GetMaxByteCount respects the MaxCharCount property of EncoderFallback
// However, Utf7Encoding ignores this
if (!(encoding is UTF7Encoding))
{
Encoding customizedMaxCharCountEncoding = Encoding.GetEncoding(encoding.CodePage, new HighMaxCharCountEncoderFallback(), DecoderFallback.ReplacementFallback);
Assert.Throws<ArgumentOutOfRangeException>("charCount", () => customizedMaxCharCountEncoding.GetMaxByteCount(2));
}
}
示例9: GetChars_Invalid
public static unsafe void GetChars_Invalid(Encoding encoding)
{
// Bytes is null
Assert.Throws<ArgumentNullException>("bytes", () => encoding.GetChars(null));
Assert.Throws<ArgumentNullException>("bytes", () => encoding.GetChars(null, 0, 0));
Assert.Throws<ArgumentNullException>("bytes", () => encoding.GetChars(null, 0, 0, new char[0], 0));
// Chars is null
Assert.Throws<ArgumentNullException>("chars", () => encoding.GetChars(new byte[4], 0, 4, null, 0));
// Index < 0
Assert.Throws<ArgumentOutOfRangeException>("index", () => encoding.GetChars(new byte[4], -1, 4));
Assert.Throws<ArgumentOutOfRangeException>("byteIndex", () => encoding.GetChars(new byte[4], -1, 4, new char[1], 0));
// Count < 0
Assert.Throws<ArgumentOutOfRangeException>("count", () => encoding.GetChars(new byte[4], 0, -1));
Assert.Throws<ArgumentOutOfRangeException>("byteCount", () => encoding.GetChars(new byte[4], 0, -1, new char[1], 0));
// Count > bytes.Length
Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetChars(new byte[4], 0, 5));
Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetChars(new byte[4], 0, 5, new char[1], 0));
// Index + count > bytes.Length
Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetChars(new byte[4], 5, 0));
Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetChars(new byte[4], 5, 0, new char[1], 0));
Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetChars(new byte[4], 4, 1));
Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetChars(new byte[4], 4, 1, new char[1], 0));
Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetChars(new byte[4], 3, 2));
Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetChars(new byte[4], 3, 2, new char[1], 0));
// CharIndex < 0 or >= chars.Length
Assert.Throws<ArgumentOutOfRangeException>("charIndex", () => encoding.GetChars(new byte[4], 0, 4, new char[1], -1));
Assert.Throws<ArgumentOutOfRangeException>("charIndex", () => encoding.GetChars(new byte[4], 0, 4, new char[1], 2));
// Chars does not have enough capacity to accomodate result
Assert.Throws<ArgumentException>("chars", () => encoding.GetChars(new byte[4], 0, 4, new char[1], 1));
byte[] bytes = new byte[encoding.GetMaxByteCount(2)];
char[] chars = new char[4];
char[] smallChars = new char[1];
fixed (byte* pBytes = bytes)
fixed (char* pChars = chars)
fixed (char* pSmallChars = smallChars)
{
byte* pBytesLocal = pBytes;
char* pCharsLocal = pChars;
char* pSmallCharsLocal = pSmallChars;
// Bytes or chars is null
Assert.Throws<ArgumentNullException>("bytes", () => encoding.GetChars((byte*)null, 0, pCharsLocal, chars.Length));
Assert.Throws<ArgumentNullException>("chars", () => encoding.GetChars(pBytesLocal, bytes.Length, (char*)null, chars.Length));
// ByteCount or charCount is negative
Assert.Throws<ArgumentOutOfRangeException>("byteCount", () => encoding.GetChars(pBytesLocal, -1, pCharsLocal, chars.Length));
Assert.Throws<ArgumentOutOfRangeException>("charCount", () => encoding.GetChars(pBytesLocal, bytes.Length, pCharsLocal, -1));
// Chars does not have enough capacity to accomodate result
Assert.Throws<ArgumentException>("chars", () => encoding.GetChars(pBytesLocal, bytes.Length, pSmallCharsLocal, smallChars.Length));
}
}
示例10: GetMaxByteCount_Invalid
public static void GetMaxByteCount_Invalid(Encoding encoding)
{
Assert.Throws<ArgumentOutOfRangeException>("charCount", () => encoding.GetMaxByteCount(-1));
if (!(encoding is ASCIIEncoding))
{
Assert.Throws<ArgumentOutOfRangeException>("charCount", () => encoding.GetMaxByteCount(int.MaxValue / 2));
}
Assert.Throws<ArgumentOutOfRangeException>("charCount", () => encoding.GetMaxByteCount(int.MaxValue));
}