C# Unicode与GB2312互换的问题
来源:互联网 发布:鸟瞰图设计软件 编辑:程序博客网 时间:2024/05/22 09:40
经过两天的研究弱于解决了
//GB2312转换成unicode编码
public string GB2Unicode(string str)
{
string Hexs = "";
string HH;
Encoding GB = Encoding.GetEncoding("GB2312");
Encoding unicode = Encoding.Unicode;
byte[] GBBytes = GB.GetBytes(str);
for (int i = 0; i < GBBytes.Length; i++)
{
HH = "%" + GBBytes[i].ToString("X");
Hexs += HH;
}
return Hexs;
}
//unicode编码转换成GB2312汉字
public string UtoGB(string str)
{
string[] ss = str.Split('%');
byte[] bs = new Byte[ss.Length - 1];
for (int i = 1; i < ss.Length; i++)
{
bs[i - 1] = Convert.ToByte(Convert2Hex(ss[i])); //ss[0]为空串
}
char[] chrs = System.Text.Encoding.GetEncoding("GB2312").GetChars(bs);
string s = "";
for (int i = 0; i < chrs.Length; i++)
{
s += chrs[i].ToString();
}
return s;
}
private string Convert2Hex(string pstr)
{
if (pstr.Length == 2)
{
pstr = pstr.ToUpper();
string hexstr = "0123456789ABCDEF";
int cint = hexstr.IndexOf(pstr.Substring(0, 1)) * 16 + hexstr.IndexOf(pstr.Substring(1, 1));
return cint.ToString();
}
else
{
return "";
}
}
//GB2312转换成unicode编码
public string GB2Unicode(string str)
{
string Hexs = "";
string HH;
Encoding GB = Encoding.GetEncoding("GB2312");
Encoding unicode = Encoding.Unicode;
byte[] GBBytes = GB.GetBytes(str);
for (int i = 0; i < GBBytes.Length; i++)
{
HH = "%" + GBBytes[i].ToString("X");
Hexs += HH;
}
return Hexs;
}
//unicode编码转换成GB2312汉字
public string UtoGB(string str)
{
string[] ss = str.Split('%');
byte[] bs = new Byte[ss.Length - 1];
for (int i = 1; i < ss.Length; i++)
{
bs[i - 1] = Convert.ToByte(Convert2Hex(ss[i])); //ss[0]为空串
}
char[] chrs = System.Text.Encoding.GetEncoding("GB2312").GetChars(bs);
string s = "";
for (int i = 0; i < chrs.Length; i++)
{
s += chrs[i].ToString();
}
return s;
}
private string Convert2Hex(string pstr)
{
if (pstr.Length == 2)
{
pstr = pstr.ToUpper();
string hexstr = "0123456789ABCDEF";
int cint = hexstr.IndexOf(pstr.Substring(0, 1)) * 16 + hexstr.IndexOf(pstr.Substring(1, 1));
return cint.ToString();
}
else
{
return "";
}
}
- C# Unicode与GB2312互换的问题
- GB2312, BIG5, UTF8, Unicode之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换
- 《UTF-8与GB2312之间的互换》
- UTF-8与GB2312之间的互换
- UTF-8与GB2312之间的互换 .
- 巫师编程语言“咒语” 设想
- PHP技巧:php过滤危险html代码
- 关于MVS的blksize
- VBscript弹出信息属性样式
- IP限制程序
- C# Unicode与GB2312互换的问题
- c#经典入门学习笔记-定义集合
- .net经典控件
- ASP.NET的错误处理机制
- PHP 截取字符串专题
- 一些比较经典的web2.0设计
- C#的四个基本技巧
- 终于决定放弃CSDN BLOG
- 一些Flex开源项目