ASP.NET

作者:互联网   出处:控件中国网   2014-11-05 19:05:17   阅读:1

ASP.NET-------字符串和字节数组转换
 
using System;
using System.Collections.Generic;
using System.Text;
namespace NET.MST.Fourth.StringByte
{
    class StringByte
    {
        static void Main(string[] args)
        {
            String s = "我是字符串,I am string";
            //字节数组转换到字符串
            Byte[] utf8 = StringToByte(s,
                 Encoding.UTF8);
            Byte[] gb2312 = StringToByte(s,
                 Encoding.GetEncoding("GB2312"));
            Byte[] unicode = StringToByte(s,
                 Encoding.Unicode);
            Console.WriteLine(utf8.Length);
            Console.WriteLine(gb2312.Length);
            Console.WriteLine(unicode.Length);
            //转换回字符串
            Console.WriteLine(ByteToString(utf8,
                 Encoding.UTF8));
            Console.WriteLine(ByteToString(gb2312,
                 Encoding.GetEncoding("GB2312")));
            Console.WriteLine(ByteToString(unicode,
                Encoding.Unicode));
            Console.Read();
        }
        static Byte[] StringToByte(String s, Encoding encoding)
        {
            return encoding.GetBytes(s);
        }
        static String ByteToString(Byte[] b, Encoding encoding)
        {
            return encoding.GetString(b);
        }
    }
}
 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat