zoukankan      html  css  js  c++  java
  • Python_Example_字符串转换实现程序

    2018-09-13

    Author: 楚格

    IDE: Pycharm2018.02   Python 3.7   

    KeyWord : 字符串

    Explain:

    Python操作数据,在串口组帧发送时,遇到数据转换的情况。

    string  如:'0102030405060708'
    bytes  如:b'x01x02x03x04x05x06x07x08'
    list           如:[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]

    1------------------------------------------------------------------------------------------------------------------

    import sys
    
    '''
    1.字符 -> 字节 -> 字符串  hex string转换为str
            hexstring ->  bytearray   ->   str
    字符串:  8     <class 'str'>           01020304
            4      <class 'bytearray'>     bytearray(b'x01x02x03x04') 
            30     <class 'str'>           bytearray(b'x01x02x03x04')
    '''
    STR_DATA='01020304'
    
    aaa = bytearray.fromhex(STR_DATA)
    bbb = str(aaa)
    
    print('字符串: ',len(STR_DATA),type(STR_DATA),STR_DATA,)
    print(len(aaa),type(aaa),aaa,'
    ',len(bbb),type(bbb),bbb)
    
    '''
    2.字符 -> 字节 -> 列表   
    hexstring -> bytearray -> list
    
    字符串:  8 <class 'str'>         01020304
            4 <class 'bytearray'>   bytearray(b'x01x02x03x04') 
            4 <class 'list'>        [1, 2, 3, 4]
            
    这转换是比较重要的
    '''
    STR_DATA='01020304'
    ccc = bytearray.fromhex(STR_DATA)
    ddd = list(ccc)
    
    print('字符串: ',len(STR_DATA),type(STR_DATA),STR_DATA,)
    print(len(ccc),type(ccc),ccc,'
    ',len(ddd),type(ddd),ddd)
  • 相关阅读:
    vue换一换功能原型
    一些文章收集
    mint-ui popup自动关闭
    vue 实现二选一列表
    用数组实现矩阵乘法
    表格
    表单
    django项目创建和结构解释
    js操作元素样式
    操作标签属性
  • 原文地址:https://www.cnblogs.com/caochucheng/p/9641155.html
Copyright © 2011-2022 走看看