字符编码(gb2312&&UTF-8)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 =string.Empty;
            string str2 = string.Empty;
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            Encoding utf8 = Encoding.GetEncoding("utf-8");
            Random rm = new Random();         
            int regionCode = rm.Next(16, 56);
            int positionCode;
            if (regionCode == 55)
            {
                // 55区排除90,91,92,93,94
                positionCode = rm.Next(1, 90);
            }
            else
            {
                positionCode = rm.Next(1, 95);
            }        
            // 转换区位码为机内码
            int regionCode_Machine = regionCode + 160;// 160即为十六进制的20H+80H=A0H
            int positionCode_Machine = positionCode + 160;// 160即为十六进制的20H+80H=A0H
         
            byte[] bytes = new byte[] { (byte)regionCode_Machine, (byte)positionCode_Machine };
            str1=gb2312.GetString(bytes);
            str2 = utf8.GetString(bytes);
           
            Console.WriteLine(str1);
            Console.WriteLine(str2);
            Console.ReadKey();
        }
    }
}

posted @ 2012-10-27 15:09  曲终人散xwz  阅读(415)  评论(0编辑  收藏  举报