将两个十六进制数连接成 python 中的字符串

concatenate two hex number into string in python

我尝试将 int 转换为 hex 并将 hex 连接成字符串并将其写入文件。

但是当我执行以下操作时:

c = ""
c = hex(a) + hex(b)

我说

时出错

'str' object cannot be interpreted as an integer.

如何在 python3.5 中解决这个问题?

已解决: 事实证明 a 是一个字符串,我想去掉前导 0x 所以将其转换为普通十六进制的更好方法是 {0:02x}.format(yourHex).

ab 应该是整数,因为 hex 需要整数,它 returns string 所以你可以连接这两个 hex().

a = 5
b = 7
c = ""
filePath = "C:\..\..\hexFile.txt"    #Path of your file
with open(filePath, "w") as file:
    c = hex(a) + hex(b)
    file.writelines(c)