本文整理汇总了C#中Encoding.GetChars方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetChars方法的具体用法?C# Encoding.GetChars怎么用?C# Encoding.GetChars使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encoding
的用法示例。
在下文中一共展示了Encoding.GetChars方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: NegativeTestChars3
public bool NegativeTestChars3(Encoding enc, byte[] bytes, int index, int count, char[] chars, int bIndex, Type excType, string id)
{
bool result = true;
TestFramework.BeginScenario(id + ": Getting bytes with encoding " + enc.WebName);
try
{
int output = enc.GetChars(bytes, index, count, chars, bIndex);
string str = new string(chars);
result = false;
TestFramework.LogError("011", "Error in " + id + ", Expected exception not thrown. Actual chars " + str + ", Expected exception type: " + excType.ToString());
}
catch (Exception exc)
{
if (exc.GetType() != excType)
{
result = false;
TestFramework.LogError("012", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
}
}
return result;
}
示例3: PositiveTestString
public bool PositiveTestString(Encoding enc, string expected, byte[] bytes, string id)
{
bool result = true;
TestFramework.BeginScenario(id + ": Getting bytes for " + Utilities.ByteArrayToString(bytes) + " with encoding " + enc.WebName);
try
{
char[] chars = enc.GetChars(bytes);
string str = new string(chars);
if (!expected.Equals(str))
{
result = false;
TestFramework.LogError("001", "Error in " + id + ", unexpected comparison result. Actual chars " + str + ", Expected: " + expected);
}
}
catch (Exception exc)
{
result = false;
TestFramework.LogError("002", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
}
return result;
}
示例4: 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));
}
}
示例5: GetChars
private static char[] GetChars(MemoryStream b, Encoding e)
{
return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
}
示例6: ConvertTo
public static string ConvertTo(Encoding srcEnc, Encoding dstEnc, string srcStr)
{
// Convert the string into a byte[].
byte[] srcBytes = srcEnc.GetBytes(srcStr);
// Perform the conversion from one encoding to the other.
byte[] dstBytes = Encoding.Convert(srcEnc, dstEnc, srcBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] dstChars = new char[dstEnc.GetCharCount(dstBytes, 0, dstBytes.Length)];
dstEnc.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);
return new string(dstChars);
}
示例7: 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();
}
示例8: 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));
}
}
示例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: VerifyGetChars
private static unsafe void VerifyGetChars(Encoding encoding, byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, char[] expectedChars)
{
char[] originalChars = (char[])chars.Clone();
// Use GetChars(byte[])
if (byteIndex == 0 && byteCount == bytes.Length)
{
char[] resultBasic = encoding.GetChars(bytes);
VerifyGetChars(resultBasic, 0, resultBasic.Length, originalChars, expectedChars);
}
// Use GetChars(byte[], int, int)
char[] resultAdvanced = encoding.GetChars(bytes, byteIndex, byteCount);
VerifyGetChars(resultAdvanced, 0, resultAdvanced.Length, originalChars, expectedChars);
// Use GetChars(byte[], int, int, char[], int)
char[] byteChars = (char[])chars.Clone();
int charCount = encoding.GetChars(bytes, byteIndex, byteCount, byteChars, charIndex);
VerifyGetChars(byteChars, charIndex, charCount, originalChars, expectedChars);
Assert.Equal(expectedChars.Length, charCount);
// Use GetCharCount(byte*, int, char*, int) - only works for non-null/non-empty byte* or char*
if (expectedChars.Length > 0)
{
char[] bytePointerChars = (char[])chars.Clone();
fixed (byte* pBytes = bytes)
fixed (char* pChars = bytePointerChars)
{
int charPointerCount = encoding.GetChars(pBytes + byteIndex, byteCount, pChars + charIndex, bytePointerChars.Length - charIndex);
Assert.Equal(expectedChars.Length, charPointerCount);
}
VerifyGetChars(bytePointerChars, charIndex, charCount, originalChars, expectedChars);
}
}
示例11: GetChars
public static void GetChars(Encoding encoding, byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, char[] expectedChars)
{
char[] originalChars = (char[])chars.Clone();
// Use GetChars(byte[])
if (byteIndex == 0 && byteCount == bytes.Length)
{
char[] resultBasic = encoding.GetChars(bytes);
VerifyGetChars(resultBasic, 0, resultBasic.Length, originalChars, expectedChars);
}
// Use GetChars(byte[], int, int)
char[] resultAdvanced = encoding.GetChars(bytes, byteIndex, byteCount);
VerifyGetChars(resultAdvanced, 0, resultAdvanced.Length, originalChars, expectedChars);
// Use GetChars(byte[], int, int, char[], int)
int charCount = encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
VerifyGetChars(chars, charIndex, charCount, originalChars, expectedChars);
Assert.Equal(expectedChars.Length, charCount);
}
示例12: GetChars_Invalid
public static 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));
}
示例13: GetChars
public static void GetChars(Encoding encoding, byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int expected)
{
int result = encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
Assert.Equal(expected, result);
}