python 学习汇总6:字节对象bytes转换(tcy)

字节对象bytes 2018 / 7 / 1

# =======================================================================
1.class bytes([source[,encoding[,errors]]] )#单个字节不可变序列
    # 字节文字语法与字符串文字的语法基本相同,只是b添加了前缀

1.1.创建字节
    b0 = bytes()                          #创建空字节 b''
    b1 = bytes(3)                        #创建3个空字节 b'\x00\x00\x00'
    b2 = bytes(range(3))            #创建3个字节 b'\x00\x01\x02'
    b3 = bytes(b1)                      # b3 is b1 True
    bytes([36, 36, 36])                # 必须为整数 b'$$$'
    bytes(str, encoding='utf-8')  # 必须有编码参数


1.2.方法
    bytes.fromhex(string )      #类方法  16字符串转字节
        # 该字符串每个字节必须包含两个十六进制数字,并忽略ASCII空格。
    types.hex()                      #字节对象转换为其十六进制str
        # 返回包含实例中每个字节的两个十六进制数字的字符串对象。
========================================================================

2.字节转换

1.1.十六进制字符型字符串转字节
    a = 'aabbccddeeff1234567890'#除此之外不能包含其他字符
    a_bytes = bytes.fromhex(a)        # b'\xaa\xbb\xcc\xdd\xee\xff\x124Vx\x90'

1.2.字节转字符串
    aa = a.hex()                               #aabbccddeeff1234567890 str
    b'\xf0\xf1\xf2'.hex()                  # 'f0f1f2'

import  binascii
str1='hello';str2='16'
2.1.字符串转字节-编码
    b1=str1.encode()                       #b'hello'
    b2=str2.encode()                       #b'16'
    bytes('hello',encoding='utf-8')  #b'hello'

2.2.字节转字符串-解码
    s1=b1.decode()                         #hello str
    s2=b2.decode('utf-8')                #16    str
    str(b'hello',encoding='utf-8')    #'hello' str

3.1.二进制字节转16进制字节
    h1b1 = binascii.b2a_hex(b1)     #b'68656c6c6f'
    h1b2 = binascii.b2a_hex(b2)     #b'3136'

3.2.十六进制字节转2进制字节
    a1b1=binascii.a2b_hex(h1b1)   #b'hello'
    a1b2=binascii.a2b_hex(h1b2)   #b'16'

4.1.hex整数转换为16进制字符串
    s=hex(-123456789)                   #'-0x75bcd15'<class 'str'>
    bin(222222)                               #'0b110110010000001110' str
    oct(8)                                         #'0o10' str
    hex(16)                                      #'0x10' str

4.2.字符串转整数
    int('12',10)                                 #12 int

5.1.浮点数转16进制字符串:
    3.0.hex()                                   #'0x1.8000000000000p+1' str
    float.hex(3.0)                            #'0x1.8000000000000p+1' str

5.2.字符串转浮点数
    float.fromhex('0x3')                  # 3.0 float
    float.fromhex('0x1.80p+1')       # 3.0 float
    3.0.fromhex('0x3')                    # 3.0 float

6.1.AscII整数转字符
    chr(97)                                     #'a' str
6.2.字符转AscII整数
    ord('a')                                     #97 int

-----------------------------------------------------------------------------------
4.字节转换为一个整数列表
    b = bytes([44, 45, 46])              # b'...'
    list(b)                                        # [44, 45, 46]

# 5.文档编码转换
#     1)现在计算机内存中采用unicode编码方式。
#     2)encode()函数:将unicode编码转换成其他的编码方式。
#          decode() 函数:将其他编码方式转换成unicode编码方式
#     3)记事本(unicode) - -->abc.txt(utf - 8)
#          abc.txt(utf - 8) - -->记事本(unicode)

# =======================================================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值