问一个字符编码的问题

xiezuming 2003-11-29 10:21:08
我用htmlDocument.createDocumentFromUrl读取了一个URL的内容
然后用htmlDocument.body.innerHTML得到了这个页面的内容
但是把它存到文件里的时候,所有的中文都是乱码
但是用editplus打开后 再另存为一个文件就好了
想知道如果用c#怎样把它转换过来?
谢谢
...全文
55 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
oop2000 2003-12-01
  • 打赏
  • 举报
回复
using System.IO;
using System.Text;

//解码
private void butEncodeorDecode_Click(object sender, System.EventArgs e)
{
this.Cursor=Cursors.WaitCursor;
try
{
string filename=@"F:\Test.htm";
FileStream Fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] bb = new byte[(int)Fs.Length];
int len = Fs.Read(bb, 0, bb.Length);
String str =Encoding.GetEncoding("gb2312").GetString(bb, 0, len);
this.Cursor=Cursors.Default;
MessageBox.Show(str);
}
catch(Exception ex)
{
this.Cursor=Cursors.Default;
MessageBox.Show(ex.Message);
}
}
joachern 2003-12-01
  • 打赏
  • 举报
回复
楼上
gujianxin 2003-12-01
  • 打赏
  • 举报
回复
1, web.config 中加入
<globalization requestEncoding="gb2312" responseEncoding="gb2312" />
你的程序就会使用正确的字符集,
2

System.Text.GetEncoding("gb2312").GetString(System.Text.Encoding.GetByte(str))

问题原因:.net 默认字符集是Utf8,而我们的页面一般是gb2312,所以要进行转化,
piziliu2003 2003-12-01
  • 打赏
  • 举报
回复
FileStream fs = new FileStream("filename", FileMode.Open, FileAccess.Read);
byte[] bb = new byte[(int)fs.Length];
int len = fs.Read(bb, 0, bb.Length);
String str = Encoding.GetEncoding("gb2312").GetString(bb, 0, len);
piziliu2003 2003-12-01
  • 打赏
  • 举报
回复
byte[] array = new byte[2];
string str =textBox2.Text;


string s1 = str.Substring(0,2);
string s2 = str.Substring(2,2);

int t1 = Convert.ToInt32(s1,16);
int t2 = Convert.ToInt32(s2,16);

array[0] = (byte)t1;
array[1] = (byte)t2;

string s = System.Text.Encoding.Default.GetString(array);
textBox1.Text=s;

速马 2003-11-30
  • 打赏
  • 举报
回复
System.Text.Encoding.xxx.GetString(System.Text.Encoding.yyy.GetByte(str))

xxx和yyy是什么要看你的string用什么编码的了
八爪鱼-杭州 2003-11-30
  • 打赏
  • 举报
回复
System.Text.Encoding
xiezuming 2003-11-29
  • 打赏
  • 举报
回复
upup
给定一个字符串,如何转换编码方式啊?

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧