How to convert a string to hex?
David Goodger
dgoodger at bigfoot.com
Thu Jun 29 23:14:21 EDT 2000
on 2000-06-29 09:16, Remco Gerlich (scarblac-spamtrap at pino.selwerd.nl)
wrote:
> Jens Arnfelt wrote in comp.lang.python:
>> Is there any easy way to go the other way, like convert hex number
>> "0x26100511FFFF" to a string with the the following value: "26100511FFFF"
>> ?
>
> x = 0x26100 # (your example is too large for my computer)
>
> y = "0x%x" % x # String formatting "%x" formatter makes hex numbers
>
> y is now "0x26100".
No whole number is too large for Python! Just attach an "L", which we all
know is the symbol of: Integer Repair Man! vis,
>>> x = 0x26100511FFFF
OverflowError: integer literal too large
>>> x = 0x26100511FFFFL # Our hero!
>>> x
41850246397951L
>>> hex(x) # hex() works where "%x"%x doesn't, with long integers
'0x26100511FFFFL'
>>> hex(x)[2:-1] # to strip off the leading "0x" and trailing "L", if desired
'26100511FFFF'
--
David Goodger dgoodger at bigfoot.com Open-source projects:
- The Go Tools Project: http://gotools.sourceforge.net
(more to come!)
More information about the Python-list
mailing list