C#编码资料汇总

259 篇文章 4 订阅
148 篇文章 1 订阅

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 = '中';  //换成"迎"时,得到的是负数,解决方法是,看下面

        short uc = 22478;
        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-8859-1,但.Net地层格处理成了Gb2312了,所以需要通过两种编码转换成Unicode才能正常显示,听说在Java中只要new String(s.getBytes("ISO-8859-1"),"gb2312");就OK了。

转换成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 = "&#32654;&#20029;";
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/6915165484

用了好几种编码 全是乱码,request.setCharacterEncoding("UTF-8");尝试了好几种

String newdefrayItem = new String(request.getParameter("newdefrayItem").getBytes("iso-8859-1"),"GBK");也换了几次编码方式,最后是String newdefrayItem = new String(request.getParameter("newdefrayItem").getBytes("iso-8859-1"),"GBK");这样可以。在网上查了下原理,转了过来

 

tomcat默认全部都是用ISO-8859-1编码,不管你页面用什么显示,Tomcat最终还是会替你将所有字符转做ISO-8859-1.那么,当在另目标页面再用GBK翻译时就会将本来错的编码翻译成GBK的编码,这时的文字会乱码.

所以需要先将得到"字符"(不管是什么)都先用字节数组表示,且使用ISO-8859-1进行翻译,得到一个在ISO-8859-1编码环境下的字节数组.例如:AB表示成[64,65].然后再用GBK编码这个数组,并翻译成一个字符串.

那么我们可以得到一个编码转换的过程
假设:GBK码("你")->URLencode后变成->(%3F%2F)->Tomcat自动替你转一次ISO-8859-1->得到( 23 43 68 23 42 68 每一个符号表示为ISO-8859-1中的一个编码)->接收页面--->再转一次为ISO-8859-1的Byte数组[23,43,68,23,42,68]--->用GBK再转为可读的文字--->(%3F%2F"---->转为("你")

 

除了UTF-16,其它字符集定义时都重复。

比如汉字“我”,假设它的值是22530(只是假设,具体多少我没查)
而日文的“マ”的值也可能是22530(也是假设)或韩文的“?”

在网络上传输是不能以高字节传输,因为网络底层最后只认无符号char,相当于java中的byte,所以
22530这个int要转换为字节数组,

byte[0] = (22530 >> 8)&0xFF;
byte[1] = 22530 &0xFF;
具体多少我没算,假设是byte[125,231]

这样的字节传到服务端到是表示汉字“我”还是日文的“マ”还是其它狗屁?
一般通讯协议中会告诉对字符集,比如HTTP在请求时告诉服务端:
ContentType="xxxxxxxxxx";charset="GKB";
这时服务端就知道现在接收到的[125,231]是GKB的“我”而不是其它文字。

上面是标准的通信过程。但如果有些水平很差的程序员在提交请求时没有通知服务端字符集,那服务端就没办法了。
只好按最常用的字符集来猜一个默认的。

这还不错,最要命的是写服务器的程序员水平和见识很差时,就要命了。就象写老版本的TOMCAT的程序员,他自己生在西方,以为全世界所有人都用的是26个字母加一些符号,所以他不管客户端提交什么都按ISO-8859-1来算,结果可想而知。

没办法,谁让我们用GBK的人不会写tomcat呢,只好先把让那个差劲的程序员错误生成的String用ISO-8859-1还原成
[125,231],再重新用GKB生成String.

 

 

用于得到服务器传来的字符重新生成GBK编码


Encoding.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 (?)





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值