python以16進制列印
發布時間: 2025-09-09 12:02:51
⑴ 用python轉換浮點數為16進制怎麼寫比如把34.4536或者-34.4536轉成16進制
#-*-coding:utf8-*-
importctypes
defh2f(s):
cp=ctypes.pointer(ctypes.c_longlong(s))
fp=ctypes.cast(cp,ctypes.POINTER(ctypes.c_double))
returnfp.contents.value
deff2h(s):
fp=ctypes.pointer(ctypes.c_double(s))
cp=ctypes.cast(fp,ctypes.POINTER(ctypes.c_longlong))
returnhex(cp.contents.value)
print(f2h(34.4536))
print(h2f(0x40413a0f9096bb99))
⑵ python中想要把字母或數字轉為16進制\x30格式並且輸出,但是最終顯示卻還是字母是怎麼回事呢
給你一個函數試試。
def str_to_hex(s):
return ' '.join([hex(ord(c)).replace('0x', '') for c in s])
⑶ python我怎麼把數組裡面的每個字元串轉換成16進制數啊
先把字元串轉化為數字格式,
再用hex()把十進度數字轉化為十六進制數
代碼如下:
source=['1','2','3','4']
destination=[]
foriteminsource:
destination.append(hex(int(item)))
print(destination)
輸出如下:
['0x1','0x2','0x3','0x4']
熱點內容