/**/
/// <summary>
/// 代码转换功能
/// </summary>
public class CodingChange
{
/**//// <summary>
/// 把字符型转换成16进制编码
/// </summary>
/// <param name="character">字符串</param>
/// <returns>一个字符转成4位编码</returns>
public static string CharacterToCoding(string character)
{
string coding = "";
for (int i = 0; i<character.Length; i++ )
{
//取出二进制编码内容
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(character.Substring(i,1));
//取出低字节编码内容(两位16进制)
string lowCode = System.Convert.ToString(bytes[0], 16);
if (lowCode.Length == 1)
lowCode = "0" + lowCode;
//取出高字节编码内容(两位16进制)
string hightCode = System.Convert.ToString(bytes[1], 16);
if (hightCode.Length == 1)
hightCode = "0" + hightCode;
coding += (lowCode + hightCode); //加入到字符串中
}
return coding;
}
/**//// <summary>
/// 把16进制编码转换成字符型
/// </summary>
/// <param name="coding">4位转成一位,长度必须是4的倍数</param>
/// <returns>字符串</returns>
public static string CodingToCharacter(string coding)
{
string characters = "";
if (coding.Length % 4 != 0)//编码为16进制,必须为4的倍数。
{
throw new System.Exception("编码格式不正确");
}
for (int i = 0; i<coding.Length; i+=4 ) //每四位为一个汉字
{
byte[] bytes = new byte[2];
string lowCode = coding.Substring(i, 2); //取出低字节,并以16进制进制转换
bytes[0] = System.Convert.ToByte(lowCode, 16);
string highCode = coding.Substring(i + 2, 2); //取出高字节,并以16进制进行转换
bytes[1] = System.Convert.ToByte(highCode, 16);
string character = System.Text.Encoding.Unicode.GetString(bytes);
characters += character;
}
return characters;
}
}
/// 代码转换功能
/// </summary>
public class CodingChange
{
/**//// <summary>
/// 把字符型转换成16进制编码
/// </summary>
/// <param name="character">字符串</param>
/// <returns>一个字符转成4位编码</returns>
public static string CharacterToCoding(string character)
{
string coding = "";
for (int i = 0; i<character.Length; i++ )
{
//取出二进制编码内容
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(character.Substring(i,1));
//取出低字节编码内容(两位16进制)
string lowCode = System.Convert.ToString(bytes[0], 16);
if (lowCode.Length == 1)
lowCode = "0" + lowCode;
//取出高字节编码内容(两位16进制)
string hightCode = System.Convert.ToString(bytes[1], 16);
if (hightCode.Length == 1)
hightCode = "0" + hightCode;
coding += (lowCode + hightCode); //加入到字符串中
}
return coding;
}
/**//// <summary>
/// 把16进制编码转换成字符型
/// </summary>
/// <param name="coding">4位转成一位,长度必须是4的倍数</param>
/// <returns>字符串</returns>
public static string CodingToCharacter(string coding)
{
string characters = "";
if (coding.Length % 4 != 0)//编码为16进制,必须为4的倍数。
{
throw new System.Exception("编码格式不正确");
}
for (int i = 0; i<coding.Length; i+=4 ) //每四位为一个汉字
{
byte[] bytes = new byte[2];
string lowCode = coding.Substring(i, 2); //取出低字节,并以16进制进制转换
bytes[0] = System.Convert.ToByte(lowCode, 16);
string highCode = coding.Substring(i + 2, 2); //取出高字节,并以16进制进行转换
bytes[1] = System.Convert.ToByte(highCode, 16);
string character = System.Text.Encoding.Unicode.GetString(bytes);
characters += character;
}
return characters;
}
}