使用python将十六进制转储到YAML中 [英] Using python to dump hexadecimals into YAML
问题描述
十六进制(0x2a)==十六进制(42)=='0x2a'
。 除非您正在寻找一种方法来说服您的现有转储函数使用十六进制而不是十进制符号...
回答下面的注释,如果问题在于你想要十六进制数字的大写字母(但小写的 0x
),那么你必须使用字符串格式。您可以选择以下任一选项:
0x%02X%42#老方法
0x { :02X}格式(42)==0x2A#新方式
,您必须明确地打印 0x
,然后是大写的至少两位数的十六进制数字,如果您的数字只有一个,则用左填充0数字。这是用 02X
格式表示的,与C的 printf
相同。
Now I'm dumping into a YAML document. It's working as it should for the most part. When I try to dump a hexadecimal such as "0x2A" it converts to 42. Isn't there any way to maintain it's hexadecimal format? A string won't work sadly. And int( 0x2A, 16) also just gives me a 42.
You may be looking for hex(0x2a) == hex(42) == '0x2a'
.
Unless you're looking for a way to convince your existing dumping function to use hexadecimal instead of decimal notation...
Answering to your comment below, if the problem is that you want upper case letters for the hexadecimal digits (but lower case for the 0x
) then you have to use string formatting. You can choose one of the following:
"0x%02X" % 42 # the old way
"0x{:02X}".format(42) == "0x2A" # the new way
In both cases, you'll have to print the 0x
explicitly, followed by a hexadecimal number of at least two digits in upper case, left-padded with a zero if your number has only one digit. This is denoted by the format 02X
, same as in C's printf
.
这篇关于使用python将十六进制转储到YAML中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!