zoukankan      html  css  js  c++  java
  • python3:数字/字符串之间的变换+字节b'S'转化

    问题:

    def string2number(str):
        """Convert a string to a number
    
        Input:  string(big-endian)
        Output: long or integer
        """
        return int(str.encode('hex'),16)
    
    
    mypresent.py", line 36, in string2number
        return int(str.encode('hex'),16)
    LookupError: 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs 

    所以特地学习python中的数字表示方法,

    binary二进制(0b101)、

    octal八进制(0o74125314)、

    decimal十进制(1223)、

    hexadecimal十六进制(0xff)

    而且可以为加下来做分组加密打下coding基础

    bin(number)

    '''

    input -- a number: 输入参数可以为二进制数、八进制数、十进制数、十六进制数

    output -- a string: 输出为以0b开头的二进制字符串

    '''

    example:

    >>> bin(0x11) 
    '0b10001'
    >>> bin(0b1010111) 
    '0b1010111'
    >>> bin(0o1234567) 
    '0b1010011100101110111'
    >>> bin(1234567)   
    '0b100101101011010000111'
    >>> bin(0x1234f567ff)
    '0b1001000110100111101010110011111111111'
    >>> bin(bin(123))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object cannot be interpreted as an integer
    >>>

    oct(number)

    '''

    input -- a number: 输入参数可以为二进制数、八进制数、十进制数、十六进制数

    output -- a string: 输出为以0o开头的二进制字符串

    '''

    example:

    >>> oct(0b1111) 
    '0o17'
    >>> oct(0o1111) 
    '0o1111'
    >>> oct(1111)   
    '0o2127'
    >>> oct(0xff1111) 
    '0o77610421'
    >>> oct(oct(0xff1111))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object cannot be interpreted as an integer
    >>>

    oct(number)

    '''

    input -- a number: 输入参数可以为二进制数、八进制数、十进制数、十六进制数

    output -- a string: 输出为以0x开头的二进制字符串

    '''

    example:

    >>> hex(0b1111)        
    '0xf'
    >>> hex(0o1111) 
    '0x249'
    >>> hex(1111)   
    '0x457'
    >>> hex(0x1111) 
    '0x1111'
    >>> hex(hex(0x1111))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object cannot be interpreted as an integer
    >>>

    int(number)

    '''

    input -- a number: 输入参数可以为二进制数、八进制数、十进制数、十六进制数、浮点数

    output -- a string: 输出整数,浮点数向下取整

    '''

    构造函数如下:

     int(x=0) --> integer

     int(x, base=10) --> integer   给定了参数base(取0,2-36) x必须是字符串形式、bytes

    example:

    >>> int('ffff',16) 
    65535
    >>> int('ffff',8)  
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 8: 'ffff'
    >>> int('ffff',36) 
    719835
    >>> int(b'ffff',18) 
    92625
    >>> int(b'ffff',16) 
    65535
    >>>

    关于字节 转字节就是encode不多bp

    字节转16进制字符串

    >>> a=b's' 
    >>> a
    b's'
    >>> a.hex()     
    '73'
    >>> type(a.hex()) 
    <class 'str'>
    >>>

    字节转数字

    <class 'str'>
    >>> ord(a)
    115
    >>> a
    b's'
    >>> type(ord(a))  
    <class 'int'>
    >>>

    字符串转字节

    >>> a='S' 
    >>> type(a) 
    <class 'str'>
    >>> a.encode()
    b'S'
    >>> a
    'S'
    >>> type(a.encode()) 
    <class 'bytes'>
    >>>

    字节转字符串

    >>> a=b'wohaimeichiufam' 
    >>> type(a) 
    <class 'bytes'>
    >>> a.decode('utf-8')
    'wohaimeichiufam'
    >>> type(a.decode('utf-8'))
    <class 'str'>
    >>>

    想将一个十六进制字符串解码成一个字节字符串或者将一个字节字符串编码成一个十六进制字符串

    >>> s=b'hello'
    >>> import binsacii
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'binsacii'
    >>> import binascii
    >>> h = binascii.b2a_hex(s)
    >>> h
    b'68656c6c6f'
    >>> binascii.a2b_hex(h)
    b'hello'
    >>>
  • 相关阅读:
    字典-dict
    队列-deque
    with-as 语句
    odoo 在"动作"("Action")菜单中添加子菜单, 点击子菜单弹窗自定义form
    odoo onchange readonly
    pyhton 连接 oracle
    PyCharm WSL 配置
    docker 安装 oracle
    docker 安装 mssql
    odoo =like
  • 原文地址:https://www.cnblogs.com/PiaYie/p/13860723.html
Copyright © 2011-2022 走看看