Created
June 8, 2021 02:12
-
-
Save rayepeng/ae54b140ee2d9efea6f2a75438eb954f to your computer and use it in GitHub Desktop.
python 十进制转十六进制字符串
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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