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' >>>
分类:
sagewithpython
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~