|
using System;
using System.Collections.Generic;
using System.Text;
namespace QiHe.CodeLib
{
public class TextEncoding
{
/// <summary>
/// check if all charaters in text are ASCII charater
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static bool FitsInASCIIEncoding(string text)
{
if(string.IsNullOrEmpty(text))return true;
byte[] bytes = Encoding.UTF8.GetBytes(text);
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] > 127)
{
return false;
}
}
return true;
}
public static bool EncodingIsRight(Encoding encoding, byte[] data)
{
string text = encoding.GetString(data);
byte[] bytes = encoding.GetBytes(text);
if (Algorithm.ArrayEqual(bytes, data))
{
return true;
}
else
{
return false;
}
}
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;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.