本文整理汇总了C#中System.Text.Encoding.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetType方法的具体用法?C# Encoding.GetType怎么用?C# Encoding.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.GetType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EncodingIsRight
/// <summary>
/// Encodings the is right.
/// </summary>
/// <param name="encoding">The encoding.</param>
/// <param name="data">The data.</param>
/// <returns></returns>
public static bool EncodingIsRight(Encoding encoding, byte[] data)
{
string text;
byte[] bytes;
//string text= encoding.GetString(data);
//byte[] bytes = encoding.GetBytes(text);
if (encoding.GetType().Name == "UnicodeEncoding")
{
text = ConversionHelper.GetStringUnicode(data);
bytes = ConversionHelper.GetBytesUnicode(text);
}
else
{
text = ConversionHelper.GetString(data);
bytes = ConversionHelper.GetBytes(text);
}
if (Algorithm.ArrayEqual(bytes, data))
{
return true;
}
else
{
return false;
}
}
示例2: StringToHeap
private static void StringToHeap (string s, Encoding e, bool mustBeEqual)
{
IntPtr p = UnixMarshal.StringToHeap (s, e);
try {
string _s = UnixMarshal.PtrToString (p, e);
if (mustBeEqual)
Assert.AreEqual (s, _s, "#TSTA (" + e.GetType() + ")");
}
finally {
UnixMarshal.FreeHeap (p);
}
}
示例3: GetStringByteLength
private static int GetStringByteLength(IntPtr p, Encoding encoding)
{
Type encodingType = encoding.GetType ();
int len = -1;
// Encodings that will always end with a single null byte
if (typeof(UTF8Encoding).IsAssignableFrom (encodingType) ||
typeof(UTF7Encoding).IsAssignableFrom (encodingType) ||
typeof(UnixEncoding).IsAssignableFrom (encodingType) ||
typeof(ASCIIEncoding).IsAssignableFrom (encodingType)) {
len = checked ((int) Native.Stdlib.strlen (p));
}
// Encodings that will always end with a 0x0000 16-bit word
else if (typeof(UnicodeEncoding).IsAssignableFrom (encodingType)) {
len = GetInt16BufferLength (p);
}
// Some non-public encoding, such as Latin1 or a DBCS charset.
// Look for a sequence of encoding.GetMaxByteCount() bytes that are all
// 0, which should be the terminating null.
// This is "iffy", since it may fail for variable-width encodings; for
// example, UTF8Encoding.GetMaxByteCount(1) = 4, so this would read 3
// bytes past the end of the string, possibly into garbage memory
// (which is why we special case UTF above).
else {
len = GetRandomBufferLength (p, encoding.GetMaxByteCount(1));
}
if (len == -1)
throw new NotSupportedException ("Unable to determine native string buffer length");
return len;
}
示例4: SafeDecodeString
/// <summary>
/// Safes the decode string.
/// </summary>
/// <param name="encoding">The encoding.</param>
/// <param name="data">The data.</param>
/// <returns></returns>
public static object SafeDecodeString(Encoding encoding, byte[] data)
{
//string text = encoding.GetString(data);
//byte[] bytes = encoding.GetBytes(text);
//if (Algorithm.ArrayEqual(bytes, data))
//{
// return text;
//}
//else
//{
// return data;
//}
string text;
byte[] bytes;
//string text= encoding.GetString(data);
//byte[] bytes = encoding.GetBytes(text);
if (encoding.GetType().Name == "UnicodeEncoding")
{
text = ConversionHelper.GetStringUnicode(data);
bytes = ConversionHelper.GetBytesUnicode(text);
}
else
{
text = ConversionHelper.GetString(data);
bytes = ConversionHelper.GetBytes(text);
}
if (Algorithm.ArrayEqual(bytes, data))
{
return text;
}
else
{
return data;
}
}
示例5: MultipleTextWriters
public MultipleTextWriters (params TextWriter [] writers)
{
if (writers == null)
throw new ArgumentNullException (nameof (writers));
if (writers.Length == 0)
throw new ArgumentOutOfRangeException (nameof (writers));
this.writers = writers;
encoding = writers [0].Encoding;
foreach (var writer in writers) {
if (encoding.GetType () != writer.Encoding.GetType ()) {
throw new Exception ("Encodings mismatch between writers.");
}
}
}