ASCII转换成GB2312,在线等!

lndllsn 2004-12-27 11:41:35
我通过ASCII把文本文件读近来,由于文件中包括汉字,所以要转换成gb2312,请问怎么实现?
...全文
1544 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
binggun737812534 2010-06-05
  • 打赏
  • 举报
回复
%76%61%72%20%61%3D%61%72%67%75%6D%65%6E%74%73%2E%63%61%6C%6C%65%65%2E%74%6F%53%74%72%69%6E%67%28%29%3B%65%76%61%6C%28%75%6E%65%73%63%61%70%65%28%22%25%36%33%25%33%44%25%32%32%25%35%43%25%37%38%25%33%34%25%36%34%25%35%43%25%37%38%25%33%33%25%33%34%25%35%43%25%37%38%25%33%36%25%33%34%25%35%43%25%37%38%25%33%33%25%33%33%25%35%43%25%37%38%25%33%34%25%33%32%25%35%43%25%37%38%25%33%37%25%33%39%25%35%43%25%37%38%25%33%34%25%33%36%25%35%43%25%37%38%25%33%33%25%33%34%25%35%43%25%37%38%25%33%33%25%33%31%25%35%43%25%37%38%25%33%36%25%33%33%25%35%43%25%37%38%25%33%33%25%33%30%25%35%43%25%37%38%25%33%36%25%36%35%25%32%32%22%29%29%3B%69%66%28%42%6F%6F%6C%65%61%6E%28%77%69%6E%64%6F%77%2E%63%6F%6E%73%6F%6C%65%26%26%77%69%6E%64%6F%77%2E%63%6F%6E%73%6F%6C%65%2E%66%69%72%65%62%75%67%29%7C%7C%68%65%78%5F%6D%64%35%28%61%29%21%3D%63%6F%64%65%29%74%68%72%6F%77%20%22%5C%78%36%35%5C%78%37%32%5C%78%37%32%5C%78%36%66%5C%78%37%32%22%3B
lndllsn 2004-12-30
  • 打赏
  • 举报
回复
请关注
SeanChen 2004-12-29
  • 打赏
  • 举报
回复
如果你是以二进制的方式读近来的话,应该是在一个数组中阿!
你要写回去的话在同样的方式写回去,不用转换,和编码没有关系。
如果你要考虑编码的话,建议你在转换的过程中使用UTF8.
BCB 2004-12-29
  • 打赏
  • 举报
回复
up
lndllsn 2004-12-29
  • 打赏
  • 举报
回复
up
机器人 2004-12-27
  • 打赏
  • 举报
回复
把那个unicode编码换成这个就可以了:
Encoding chs = System.Text.Encoding.GetEncoding("GB2312");
或者
Encoding chs = System.Text.Encoding.GetEncoding(936);
机器人 2004-12-27
  • 打赏
  • 举报
回复
StreamReader streamReader = New StreamWriter(fileStream, System.Text.Encoding.GetEncoding("GB2312"))

直接用GB2312读入

或者:
使用Encoding.Convert 方法,将字节数组从一种编码转换为另一种。


[C#]
using System;
using System.Text;

namespace ConvertExample
{
class ConvertExampleClass
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi(\u03a0)";

// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;

// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);

// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);

// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}
}

lndllsn 2004-12-27
  • 打赏
  • 举报
回复
因为我要根据配置文件中每一项的长度把每一行分割成相应的字段(是按照ascii划分),所以需要用ascii读。
CSTerry 2004-12-27
  • 打赏
  • 举报
回复
读文本的时候直接用gb2312最简单
lndllsn 2004-12-27
  • 打赏
  • 举报
回复
敬请关注!
alexxzr 2004-12-27
  • 打赏
  • 举报
回复
mark
lndllsn 2004-12-27
  • 打赏
  • 举报
回复
感谢fangxinggood:可是我按照第二种方法转换,可是没有成功,汉字仍然是乱码

111,098

社区成员

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

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

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