夫天地者,万物之逆旅;光阴者,百代之过客。而浮生若梦,为欢几何?
GBK,utf-8,utf-7,utf-16 的实际区别

 static void Main(string[] args)
        {
            string m_String = "a联通1";

            //-------------------------------------------------------------------------------
            PrintBinary(m_String, "GBK"); //GBK 与 gb2312 结果相同
            //GBK GB2312
            //01100001 11000001 10101010 11001101 10101000 00110001
            //       a                                 联                            通             1
            // gbk和bg2312 在存储字母和数字时 首位为零,汉子首位为1,
            //===============================================================================


            //-------------------------------------------------------------------------------
            PrintBinary(m_String, "utf-8");
            //GBK GB2312
            //01100001 11101000 10000001 10010100 11101001 10000000 10011010 00110001
            //       a                                                 联                                           通             1
            // utf-8 存储字母和数字的时候用一个字节,首位可以为1,存储汉子是三个字节
            //===============================================================================


            //-------------------------------------------------------------------------------
            //UTF-7每个字节只用7位(最高位总是0)。这是为了一些特殊场景的兼容,
            //因为最开始的字符编码(ASCII)只用了低七位,
            //有些原始的邮件服务器则会自作聪明的去掉最高位编码再编码传输节省流量,
            //UTF7就是为了兼容这种设备
            PrintBinary(m_String, "utf-7");
            //GBK GB2312
            //01100001 00101011 01100111 01000110 01010011 01010001 01000111 01100111 00101101 00110001
            //       a                                                                联                                                           通              1
            //===============================================================================


            //-------------------------------------------------------------------------------
            PrintBinary(m_String, "utf-16");
            // utf-16 与 UNICODE相同
            //GBK GB2312
            //01100001 00000000 01010100 10000000 00011010 10010000 00110001 00000000
            //            a                                            联                           通              1
            // utf-16存储数字、字母、是两个字节,汉字是三个字节
            //===============================================================================

            //-------------------------------------------------------------------------------
            //txt文件编码格式
            //头2个字节如果是 fe ff 表示编码格式为unicode big endian;
            //头2个字节如果是 ff fe 表示 unicode;
            //头3个字节如果是 ef bb bf 表示 utf-8;
            //如果什么都没有就是ansi
            //===============================================================================
            Console.ReadKey();
        }

        /// <summary>
        /// 将字符串打印成二进制字符
        /// </summary>
        /// <param name="p_String">待打印的字符串</param>
        /// <param name="p_Encode">编码格式</param>
        private static void PrintBinary(string p_String, string p_Encode)
        {
            Console.WriteLine();
            Console.WriteLine($"Encode:{p_Encode},String:{p_String}");
            Console.WriteLine();
            //获取编码
            Encoding m_Encoding = Encoding.GetEncoding(p_Encode);
            //获取字节编码
            byte[] m_GBK_Byte = m_Encoding.GetBytes(p_String);
            //循环打印
            foreach (byte t_Byte in m_GBK_Byte)
            {
                Console.Write(Convert.ToString(t_Byte, 2).PadLeft(8, '0') + " ");
            }
            Console.WriteLine();
            Console.WriteLine();
        }


作者:暗夜余晖

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

2

支持

0

反对

posted @2017-11-15  拜读(1372)

评论列表

评论内容:



喜欢请打赏

支付宝 微信

请放心支付