編碼轉換

zpqiong 2006-03-20 12:33:14
如何在C#中進行編碼轉換,將所有類型的輸入文檔轉換成UNICODE類型.
...全文
172 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2006-03-23
  • 打赏
  • 举报
回复
StreamReader,例如:
StreamReader sr = new StreamReader("TestFile.txt");

zpqiong 2006-03-23
  • 打赏
  • 举报
回复
謝謝!!問題是,文本文件怎麼讀入呢?
LixingTie 2006-03-23
  • 打赏
  • 举报
回复
不多说,例子一个。

using System;
using System.Text;
using System.IO;

namespace 控制台程序
{
class Program
{
static void Main(string[] args)
{
//在C盘的根目录下创建一下名为ABC.txt的文件,并向其中写入字符。
StreamWriter writer = new StreamWriter(@"C:\ABC.txt");
writer.WriteLine("This");
writer.WriteLine("is some text");
writer.WriteLine("to test");
writer.WriteLine("Reading");
writer.Close();

//把ABC.txt的内容读出来储存在StringBuilder sb中,并打印出来。
StringBuilder sb=new StringBuilder();
StreamReader reader = File.OpenText(@"C:\ABC.txt");
do
{
sb.Append(reader.ReadLine()+"\n");
} while (reader.Peek() >= 0);
reader.Close();
Console.WriteLine(sb.ToString());
}
}
}
zpqiong 2006-03-23
  • 打赏
  • 举报
回复
Thanks for your great help!
Knight94 2006-03-22
  • 打赏
  • 举报
回复
UnicodeEncoding.Unicode.GetBytes // string to bytes
UnicodeEncoding.Unicode.GetString // Bytes to string
LixingTie 2006-03-22
  • 打赏
  • 举报
回复
string str = "this is test string!这是汉字.";
Encoding gb = Encoding.GetEncoding("GB18030");
Encoding un = Encoding.Unicode;
byte[] gbytes = gb.GetBytes(str);
byte[] ubytes = un.GetBytes(str);
Console.WriteLine(gb.GetString(gbytes));
Console.WriteLine(un.GetString(gbytes));
Console.WriteLine(un.GetString(ubytes));

使用之前,先引用命名空间system.text
livode 2006-03-22
  • 打赏
  • 举报
回复
用System.IO.StreamReader读文件时,将默认作UTF-8编码转换。
zpqiong 2006-03-22
  • 打赏
  • 举报
回复
怎麼沒人回答啊?...:((
zpqiong 2006-03-21
  • 打赏
  • 举报
回复
是文本文件的編碼轉換
zpqiong 2006-03-20
  • 打赏
  • 举报
回复
好像?肯定嗎?難道只要將輸入的文檔轉換成string類型???????
jiangchunming 2006-03-20
  • 打赏
  • 举报
回复
好像C#中的String都是Unicode编码类型的吧
zpqiong 2006-03-20
  • 打赏
  • 举报
回复
謝謝大家.控制台程序,怎麼指定?
lovefootball 2006-03-20
  • 打赏
  • 举报
回复
你读写文件的时候可以指定编码
lovefootball 2006-03-20
  • 打赏
  • 举报
回复
所有類型的輸入文檔
-------
是文件么?
你读的时候
gp341 2006-03-20
  • 打赏
  • 举报
回复
string里是unicode编码, 你用c#的入得时候直接会转换进
ascii编码得是 char[]
string类有方法可以互相转换城char[]数组

110,560

社区成员

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

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

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