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'
>>>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM