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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
This should do it:
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: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 format02X
, same as in C'sprintf
.Representing all
int
in hex format, without aHexInt
class, can be done using:With reference to this answer.