C#中将文件保存为utf-8无bom格式
http://www.csharpwin.com/csharpspace/11628r8120.shtml
- /// <summary>
- /// 保存生成html页面到相应路径
- /// </summary>
- /// <param name="Shtml">要写入的内容</param>
- /// <param name="TemplatePath">存储路径</param>
- /// <returns></returns>
- public static void WriteHtmlFile(string Shtml, string HtmlPath)
- {
- StreamWriter sw = null;
- try
- {
- UTF8Encoding utf8 = new UTF8Encoding(false);
- using (sw = new StreamWriter(HtmlPath, false, utf8))
- {
- sw.Write(Shtml);
- }
- }
- catch
- {
- throw new Exception("存储路径错误,请检查路径" + HtmlPath + "是否存在!");
- }
- finally
- {
- sw.Close();
- }
- }
Gb2312到UTF-8的字符编码转换
http://www.csharpwin.com/csharpspace/11630r5488.shtml- public string GB2312ToUTF8(string str)
- {
- Encoding uft8 = Encoding.GetEncoding(65001);
- Encoding gb2312 = Encoding.GetEncoding("gb2312");
- byte[] temp = uft8.GetBytes(str);
- byte[] temp1 = Encoding.Convert(uft8, gb2312, temp);
- string result = gb2312.GetString(temp1);
- return result;
- }
string编码的理解收藏
http://www.cnblogs.com/wenpi51/archive/2009/02/24/1397126.html
误区:
1.入门C#时经常看到这样的描述:.NET中的String都是Unicode编码。
在入门之后没太看这样的基础书并且多接触一些编码问题后,我的潜意识总觉得String有很多种编码,utf8,unicode,ascii等,并且不认为C#中有gb2312编码。
2.System.Text.Encoding.Default似乎可以解决一切编码,因为我每次用Default.GetString()来读取流中的字符串都成功!所以Default应该是根据字节的编码方式而改变的,比如如果你的字节序是ascii编码,那么Default就是ascii编码。
只到前几天在CSDN的Java社区看到一个编码问题,外加自己的几个小时的实验,终于对编码问题理清了头绪。
简短描述:
.NET中的String确实只有Unicode一种。所以编码格式的字节序列转换成String时最终都是以Unicode表示。转换成String后它以后的编码格式已经不重要或者说没有意义了。
System.Text.Encoding.Default是取系统的当前ANSI代码页的编码(MSDN上抄的),即当前系统的编码。(在我们的机子上一般都是"gb2312")这就是我经常用Default读取包括中文的文件流都正确,且必须用Default读取才正确的原因----其实用Encoding.GetEncoding("GB2312")也一样。
详细介绍:
在每次进行byte[]-->String(other-->Unicode)和String-->byte[](Unicode-->other)时都会有编码转换。
通常的转换都有设置编码的地方,如StreamReader(string path [, System.Text.Encoding encoding]),Response.Charset,这就相当于你告诉系统byte[]是什么编码,这时候.NET用你指定的编码方式去解码,然后转换成Unicode编码方式的String.
也就是说,不管何时,我们所指定的编码都是针对byte[]。
即Encoding.UTF8.GetString(byte[] buffer)是告诉系统buffer的编码是UTF8。
byte[] buf = Encoding.UTF8.GetBytes(string str)是告诉系统返回的buf编码方式是UTF8。
你可能告诉系统一个假的编码方式,或者你没有告诉系统并且byte[]的编码不是用的默认编码,那么系统解码仍然会用指定编码方式进行,在机器看来他仍然解码成功,然后转换成Unicode编码,因为机器只知道0,1字节序,他不知道解码出来的东西是否是混乱的,在他看来一切正常。
但我们去看,就会发现字符串成了一些莫名其妙的符号而没有任何意义,这就是所谓的乱码。
就好比:"you yi ge",本来是拼音"有一个",你却偏偏告诉别人这是英语,别人用英语去拼,就不知道是怎么回事了,就成了乱码,呵呵。
即:字节序是按指定编码方式编码,它有一个特定的编码方式,但它本身是中性的,不含有任何编码信息。
编码方式理论上是独立于语言的,但实际上需要语言去支持。如JAVA中有"GBK"(gb2312扩展)编码,但.NET中没有。你用Encoding.GetEncoding("GBK")会抛异常。
C#中字符的 ASCII 码和 Unicode 码收藏收藏
http://www.cnblogs.com/wenpi51/archive/2009/02/24/1397128.html
很多时候我们需要得到一个英文字符的 ASCII 码,或者一个汉字字符的 Unicode 码,或者从相关的编码查询它是哪一个字符的编码。很多人,尤其是从 VB 程序序转过来学 C# 的人,会报怨 C# 里为什么没有提供现成的函数来做这个事情——因为在 VB 中有 Asc() 函数和 Chr() 函数用于这类转换。
但是如果你学过 C,你就会清楚,我们只需要将英文字符型数据强制转换成合适的数值型数据,就可以得到相应的 ASCII 码;反之,如果将一个合适的数值型数据强制转换成字符型数据,就可以得到相应的字符。
C# 中字符的范围扩大了,不仅包含了单字节字符,也可以包含双字节字符,如中文字符等。而在字符和编码之间的转换,则仍延用了 C 语言的做法——强制转换。不妨看看下面的例子
private void TestChar() {
char ch = 'a'; short ii = 65;
this.textBox1.Text = "";
this.textBox1.AppendText("The ASCII code of \'" + ch + "\' is: " + (short) ch + "\n");
this.textBox1.AppendText("ASCII is " + ii.ToString() + ", the char is: " + (char) ii + "\n");
char cn = '中'; short uc = 22478;
this.textBox1.AppendText("The Unicode of \'" + cn + "\' is: " + (short) cn + "\n");
this.textBox1.AppendText("Unicode is " + uc.ToString() + ", the char is: " + (char) uc + "\n");
}
它的运行结果是
The ASCII code of 'a' is: 97
ASCII is 65, the char is: A
The Unicode of '中' is: 20013
Unicode is 22478, the char is: 城
从这个例子中,我们便能非常清楚的了解——通过强制转换,可以得以字符的编码,或者得到编码表示的字符。如果你需要的不是 short 型的编码,请参考第 1 条进行转换,即可得到 int 等类型的编码值
本人实验,请注意的问题是wenpi51
private void TestChar() {
char ch = 'a';
short ii = 65;
this.textBox1.Text = "";
this.textBox1.AppendText("The ASCII code of \'" + ch + "\' is: " + (short) ch + "\n");
this.textBox1.AppendText("ASCII is " + ii.ToString() + ", the char is: " + (char) ii + "\n");
char cn = '中'; //换成"迎"时,得到的是负数,解决方法是,看下面
this.textBox1.AppendText("The Unicode of \'" + cn + "\' is: " + (short) cn + "\n"); //(short)cn换成(ushort)cn
this.textBox1.AppendText("Unicode is " + uc.ToString() + ", the char is: " + (char) uc + "\n");
}
C#(.Net) 解决Informix中文乱码问题 (转载tigertian)
http://www.cnblogs.com/majian714/archive/2011/10/28/2227995.html
- public static string GetEncodingString(string srcString)
- {
- Encoding e8859Encode = Encoding.GetEncoding("iso-8859-1");
- Encoding srcEncode = Encoding.GetEncoding("gb2312");
- Encoding dstEncode = Encoding.Unicode;
- byte[] srcBytes = e8859Encode.GetBytes(srcString);//用iso-8859-1去转换源字符串
- byte[] dstBytes = Encoding.Convert(srcEncode, dstEncode, srcBytes);//但是,是从gb2312转到unicode的
- char[] dstChars = new char[dstEncode.GetCharCount(dstBytes, 0, dstBytes.Length)];
- dstEncode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);
- return new string(dstChars);
- }
转换成ISO
- public static string GetISOEncodedString(string srcString)
- {
- if(bDBInfor) {
- Encoding e8859Encode = Encoding.GetEncoding("iso-8859-1");
- Encoding srcEncode = Encoding.Unicode;
- Encoding dstEncode = Encoding.GetEncoding("gb2312");
- byte[] srcBytes = srcEncode.GetBytes(srcString);//用Unicode去转换源字符串
- byte[] dstBytes = Encoding.Convert(srcEncode, dstEncode, srcBytes);//但是,是从gb2312转到unicode的
- char[] dstChars = new char[e8859Encode.GetCharCount(dstBytes, 0, dstBytes.Length)];
- e8859Encode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);
- return new string(dstChars);
- }
- return srcString;
- }
C#字符串与ISO-8859-1字符互相转换
http://www.bhcode.net/article/20081213/3637.html
- /// <summary>
- /// 将原始字符串转换为格式为&#....;&#....
- /// </summary>
- /// <param name="srcText"></param>
- /// <returns></returns>
- private string StringToISO_8859_1(string srcText)
- {
- string dst = "";
- char[] src = srcText.ToCharArray();
- for (int i = 0; i < src.Length; i++)
- {
- string str = @"&#" + (int)src[i] + ";";
- dst += str;
- }
- return dst;
- }
- /// <summary>
- /// 将字串&#....;&#....;格式字串转换为原始字符串
- /// </summary>
- /// <param name="srcText"></param>
- /// <returns></returns>
- private string ISO_8859_1ToString(string srcText)
- {
- string dst = "";
- string[] src = srcText.Split(';');
- for (int i = 0; i < src.Length; i++)
- {
- if (src[i].Length > 0)
- {
- string str = ((char)int.Parse(src[i].Substring(2))).ToString();
- dst += str;
- }
- }
- return dst;
- }
c#转码iso-8859-1转utf-8汉字
string str = "美丽";
this.dropdownlist1.items.add(httputility.htmldecode(str));
this.label1.text = str;
C# to Convert iso-8859-1 to utf-8
http://www.chilkatsoft.com/p/p_203.asp
Here’s a C# example program to convert from iso-8859-1 to utf-8:
- // Convert iso-8859-1 to utf-8.
- // C# strings are Unicode, so we must first get a byte
- // array containing iso-8859-1 bytes.
- System.Text.Encoding iso_8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1");
- System.Text.Encoding utf_8 = System.Text.Encoding.UTF8;
- // This is our Unicode string:
- string s_unicode = "abcéabc";
- // Convert it to iso-8859-1 bytes
- byte[] isoBytes = iso_8859_1.GetBytes(s_unicode);
- // Convert the bytes to utf-8:
- byte[] utf8Bytes = System.Text.Encoding.Convert(iso_8859_1, utf_8, isoBytes);
- // Let's examine both byte arrays as hex strings.
- // We're using Chilkat.Crypt2 for simplicity...
- Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
- textBox1.Text = crypt.Encode(isoBytes, "hex");
- textBox2.Text = crypt.Encode(utf8Bytes, "hex");
- // The result is this:
- // isoBytes = 616263<strong>E9</strong>616263
- // utf8Bytes = 616263<strong>C3A9</strong>616263
- // The é character is 0xE9 in iso-8859-1, but is represented
- // as two bytes (0xC3 0xA9) in utf-8.
new String(getBytes(ISO-8859-1),GBK)解决中文乱码问题分析
http://blog.renren.com/share/341684918/6915165484Encoding.Convert 方法 (Encoding, Encoding,Byte[])
- using System;
- using System.Text;
- class Example
- {
- 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 array.
- 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.
- 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);
- }
- }
- // The example displays the following output:
- // Original string: This string contains the unicode character Pi (Π)
- // Ascii converted string: This string contains the unicode character Pi (?)