• 编码方式及转换


    ASCII码    用八位二进制表示一个英文  一个字节

    unicode    中英文均用32位二进制表示  四个字节

    utf-8    英  8位  一个字节

               中   24位   三个字节

    gbk    英   8位  一个字节

              中   16位   两个字节

    1、各个编码之间的二进制不能相互识别

    2、文件的储存,传输不能是unicode

    #encode 编码  将str---->bytes

    英文

    s1 = 'changchun'

    s11 = s1.encode('utf-8')

    s12 = s1.encode('gbk')

    中文

    s2 = '长春'

    s21 = s2.encode('utf-8')

    s22 = s2.encode('gbk')

    ascii
                A : 00000010  8位 一个字节
    
    unicode     A : 00000000 00000001 00000010 00000100 32位  四个字节
                中:00000000 00000001 00000010 00000110 32位  四个字节
    
    
    utf-8      A :  00100000 8位 一个字节
              中 :  00000001 00000010 00000110 24位 三个字节
    
    
    gbk        A : 00000110  8位 一个字节
             中  : 00000010 00000110 16位 两个字节
    1,各个编码之间的二进制,是不能互相识别的,会产生乱码。
    2,文件的储存,传输,不能是unicode(只能是utf-8 utf-16 gbk,gb2312,asciid等)
    
    py3:
        str 在内存中是用unicode编码。
            bytes类型
            对于英文:
                 str  :表现形式:s = 'alex'
                        编码方式: 010101010  unicode
                bytes :表现形式:s = b'alex'
                        编码方式: 000101010  utf-8 gbk。。。。
    
            对于中文:
                 str  :表现形式:s = '中国'
                        编码方式: 010101010  unicode
                bytes :表现形式:s = b'xe91e91e01e21e31e32'
                        编码方式: 000101010  utf-8 gbk。。。。
  • 相关阅读:
    leetcode5 Longest Palindromic Substring
    leetcode17 Letter Combinations of a Phone Number
    leetcode13 Roman to Integer
    leetcode14 Longest Common Prefix
    leetcode20 Valid Parentheses
    leetcode392 Is Subsequence
    leetcode121 Best Time to Buy and Sell Stock
    leetcode198 House Robber
    leetcode746 Min Cost Climbing Stairs
    tomcat下使用druid配置jnid数据源
  • 原文地址:https://www.cnblogs.com/aj-AJ/p/10793371.html
Copyright © 2020-2023  润新知