使用python将十六进制转储到YAML中 [英] Using python to dump hexadecimals into YAML

查看:302
本文介绍了使用python将十六进制转储到YAML中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在倾销YAML文件。它的工作方式大部分都是应该的。当我尝试转储十六进制,如0x2A它将转换为42.是否有任何方法来保持它的十六进制格式?一个字符串不会伤心。而且int(0x2A,16)也只给了我一个42。

您可能正在寻找十六进制(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆