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')) #中国人
  • 相关阅读:
    Spring Boot 应用监控
    学习学习SpringSecurity
    Spring Cloud 简介
    thinkphp 请求
    八、主从复制
    七、AOF 持久化
    五、五大数据类型实现原理
    六、RDB 持久化
    四、redis的底层数据结构
    三、五大数据类型详细用法
  • 原文地址:https://www.cnblogs.com/940531gbh/p/11270587.html
Copyright © 2011-2022 走看看