How to convert an int to a hex string?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping
--
Chapters
00:00 Question
00:39 Accepted answer (Score 278)
01:20 Answer 2 (Score 153)
01:36 Answer 3 (Score 86)
01:52 Answer 4 (Score 72)
02:34 Thank you
--
Full question
https://stackoverflow.com/questions/2269...
Answer 2 links:
[hex()]: http://docs.python.org/library/functions...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string #hex #int
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping
--
Chapters
00:00 Question
00:39 Accepted answer (Score 278)
01:20 Answer 2 (Score 153)
01:36 Answer 3 (Score 86)
01:52 Answer 4 (Score 72)
02:34 Thank you
--
Full question
https://stackoverflow.com/questions/2269...
Answer 2 links:
[hex()]: http://docs.python.org/library/functions...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string #hex #int
#avk47
ACCEPTED ANSWER
Score 289
You are looking for the chr function.
You seem to be mixing decimal representations of integers and hex representations of integers, so it's not entirely clear what you need. Based on the description you gave, I think one of these snippets shows what you want.
>>> chr(0x65) == '\x65'
True
>>> hex(65)
'0x41'
>>> chr(65) == '\x41'
True
Note that this is quite different from a string containing an integer as hex. If that is what you want, use the hex builtin.
ANSWER 2
Score 158
This will convert an integer to a 2 digit hex string with the 0x prefix:
strHex = "0x%0.2X" % integerVariable
ANSWER 3
Score 95
What about hex()?
hex(255) # 0xff
If you really want to have \ in front you can do:
print '\\' + hex(255)[1:]
ANSWER 4
Score 51
Try:
"0x%x" % 255 # => 0xff
or
"0x%X" % 255 # => 0xFF
Python Documentation says: "keep this under Your pillow: http://docs.python.org/library/index.html"