zoukankan      html  css  js  c++  java
  • 编码的进阶(重要)

    编码的进阶

    1. 不同的密码本之间不能互相识别
    2. 数据在内存中全部都是以Unicode编码的,但是当你的数据用于网络传输或者存储到硬盘中,必须是以非Unicode编码(utf-8,gbk等等)
      • 英文:
        str:'hello'
        内存中的编码方式:Unicode
        表现形式:'hello'
        bytes:
        内存中的编码方式:非Unicode
        表现形式:b'hello'

      • 中文:
        str:'中国'
        内存中的编码方式:Unicode
        表现形式:'中国'
        bytes:
        内存中的编码方式:非Unicode #utf-8
        表现形式:b'xe4xb8xadxe5x9bxbd'
        具体代码:
        #只针对于英文
        s = b'hello world' #bytes类型 不是字符串
        print(s,type(s)) #b'hello world' <class 'bytes'>
        s1 = s[1:]
        print(s1) #b'ello world' bytes类型切片

        #中文条件下
        b = '中国'
        b1 = b.encode('utf-8')
        print(b1)  #b'xe4xb8xadxe5x9bxbd' 中文下的utf-8编码
        
        #str 转换为 bytes
        s2 = '中华'  #encode  编码
        x2 = s2.encode('utf-8')
        print(x2)  #b'xe4xb8xadxe5x8dx8e'
        
        #bytes 转换为 str
        #decode 解码
        s3 = b'xe4xb8xadxe5x8dx8e'
        print(s3.decode('utf-8'))  #中华
        
        #utf-8转化为 gbk
        s4 = '中国人'
        xx = s4.encode('gbk')
        print(xx)  #b'xd6xd0xb9xfaxc8xcb'
        #gbk 转化为 utf-8
        xx1 = xx.decode('gbk')
        print(xx1) #中国人
        x11 = xx1.encode('utf-8')
        print(x11)  #b'xe4xb8xadxe5x9bxbdxe4xbaxba'
        print(x11.decode('utf-8')) #中国人
  • 相关阅读:
    Linux日志不记录问题
    Centos下yum安装PHP
    centos yum update kernel
    oh-my-zsh主题
    centos 6.6 使用tomcat6部署solr5.3.1
    Nginx manifest 实现 HTML5 Application Cache
    -bash: /bin/rm: Argument list too long
    linux mysql-5.6.26 安装
    LVM 管理减少swap分区空间增加到根分区
    Linux 使用iftop命令查看服务器流量
  • 原文地址:https://www.cnblogs.com/940531gbh/p/11270587.html
Copyright © 2011-2022 走看看