Python内置函数-2 hex, oct, chr, ord


'''
    hex() 函数用于将10进制整数转换成16进制,返回字符串类型的数据。
'''
ret_hex = hex(100)
print(ret_hex, type(ret_hex))   # 0x64 <class 'str'>


'''
    oct() 函数将一个整数转换成8进制字符串。返回字符串类型的数据'''
ret_oct = oct(100)
print(ret_oct, type(ret_oct))   # 0o144 <class 'str'>


'''
    chr() 
    参数:   用一个范围在 range(256)内的(就是0~255)整数作参数,
    返回值: 是当前整数对应的ASCII字符。
'''
ret_chr_10 = chr(100)       # d
ret_chr_16 = chr(0x64)      # d


''' 
    ord()
    参数:   它以一个字符(长度为1的字符串)作为参数,
    返回值: 返回对应的ASCII数值
    注意:   如果参数超出Python定义范围,则会引发一个 TypeError 的异常
'''
ret_ord = ord('d')     # 100

猜你喜欢

转载自blog.csdn.net/qq_42327755/article/details/86596696