Skip to content

Instantly share code, notes, and snippets.

@rayepeng
Created June 8, 2021 02:12
Show Gist options
  • Select an option

  • Save rayepeng/ae54b140ee2d9efea6f2a75438eb954f to your computer and use it in GitHub Desktop.

Select an option

Save rayepeng/ae54b140ee2d9efea6f2a75438eb954f to your computer and use it in GitHub Desktop.
python 十进制转十六进制字符串
def To_hex_str(num):
chaDic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
hexStr = ""
if num < 0:
num = num + 2**32
while num >= 16:
digit = num % 16
hexStr = chaDic.get(digit, str(digit)) + hexStr
num //= 16
hexStr = chaDic.get(num, str(num)) + hexStr
return hexStr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment