.NET IO(四) Encoding 之二

先看以下Encoding的定义,命名空间和继承关系:(位于System.Text命名空间,继承自Object)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Encoding : ICloneable

Encoding是个抽象类,系统默认的给他提供了几个实现类。

Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. In contrast, decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters.

编码就是把Unicode字符集转化为字节串的过程。反之,解码就是把经过编码的字节串转化为Unicode字符的过程。

Encoding类提供了几个属性:Default,Unicode,ASCII,UTF8等,属性都返回的还是Encoding类的对象。

如果这些仍然不能满足要求,可以使用GetEncoding来返回具体的Encoding对象。

GetEncoding(Int32)Returns the encoding associated with the specified code page identifier.
GetEncoding(String) Returns an encoding associated with the specified code page name.

Encoding中重要的方法为GetEncoder,GetDecoder,GetBytes,GetBytesCount,GetChars,GetCharsCount等。其中前两个分别取得对应的编码器和解码器。后四个取得编码和解码的Byte或者Char数。其实这些分别定义在Decoder类和Encoder类中。但是这两个类均为抽象类,要想获得其实例,需要通过Encoding的get方法取得。

 下面是MSDN中提供的一个例子,列出了怎么实现从一种编码到另一种编码的转换

    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);
            Console.Read();
        }
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值