Reputation: 221
I have some hex number such as 0x61cc1000
and I want to input them to a function which only takes string. I want to treat the hex numbers as strings without any change.
If I use str()
function it converts it to int
and then treats it as string. But I want to keep the original hex value.
Upvotes: 3
Views: 392
Reputation: 95948
Your problem is that you're using str
:
>>> str(0x61cc1000)
'1640763392' # int value of the hex number as a string
That's because first 0x61cc1000 is evaluated as an int
, then str
performed on the resulted int
.
You want to do:
"{0:x}".format(0x61cc1000)
Or
'{:#x}'.format(0x61cc1000)
As already stated in other answer, you can simply:
>>> hex(0x61cc1000)
'0x61cc1000'
See 6.1.3.1. Format Specification Mini-Language for details.
Upvotes: 4
Reputation: 3273
If you want to have 0x at the beginning you may use #x
format like this:
'{:#x}'.format(74954)
Upvotes: 1
Reputation: 47770
If you want the hex string representation of any integer, just pass it through the hex
built-in.
>>> n = 0x61cc1000
>>> n
1640763392
>>> hex(n)
'0x61cc1000'
Upvotes: 2