I want to take an integer [that will be >> 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.
answered Feb 16, 2010 at 0:10
Mike GrahamMike Graham
70.9k14 gold badges97 silver badges129 bronze badges
3
This will convert an integer to a 2 digit hex string with the 0x prefix:
strHex = "0x%0.2X" % integerVariable
answered Feb 16, 2010 at 0:17
Greg BrayGreg Bray
14.4k10 gold badges80 silver badges102 bronze badges
1
What about hex[]
?
hex[255] # 0xff
If you really want to have \
in front you can do:
print '\\' + hex[255][1:]
answered Feb 16, 2010 at 0:12
Felix KlingFelix Kling
769k171 gold badges1068 silver badges1114 bronze badges
1
Let me add this one, because sometimes you just want the single digit representation
[ x
can be lower, 'x', or uppercase, 'X', the choice determines if the output letters are upper or lower.]:
'{:x}'.format[15]
> f
And now with the new f''
format strings you can do:
f'{15:x}'
> f
To add 0 padding you can use 0>n
:
f'{2034:0>4X}'
> 07F2
NOTE: the initial 'f' in
f'{15:x}'
is to signify a format string
answered Sep 20, 2017 at 0:33
monkutmonkut
40.3k23 gold badges118 silver badges148 bronze badges
2
Try:
"0x%x" % 255 # => 0xff
or
"0x%X" % 255 # => 0xFF
Python Documentation says: "keep this under Your pillow: //docs.python.org/library/index.html"
answered Feb 16, 2010 at 0:11
DawidDawid
4,0022 gold badges26 silver badges30 bronze badges
1
If you want to pack a struct with a value = 3.6, use f-string formatting:
>>> x = 114514
>>> f'{x:0x}'
'1bf52'
>>> f'{x:#x}'
'0x1bf52'
answered Apr 5 at 4:57
Xinyi LiXinyi Li
6026 silver badges9 bronze badges
1
This worked best for me
"0x%02X" % 5 # => 0x05
"0x%02X" % 17 # => 0x11
Change the [2] if you want a number with a bigger width [2 is for 2 hex printned chars] so 3 will give you the following
"0x%03X" % 5 # => 0x005
"0x%03X" % 17 # => 0x011
answered Jun 10, 2017 at 19:14
shakram02shakram02
9,5323 gold badges20 silver badges21 bronze badges
[int_variable].to_bytes[bytes_length, byteorder='big'|'little'].hex[]
For example:
>>> [434].to_bytes[4, byteorder='big'].hex[]
'000001b2'
>>> [434].to_bytes[4, byteorder='little'].hex[]
'b2010000'
answered Jan 30, 2019 at 6:53
I wanted a random integer converted into a six-digit hex string with a # at the beginning. To get this I used
"#%6x" % random.randint[0xFFFFFF]
answered Jan 22, 2012 at 22:15
ncmathsadistncmathsadist
4,5343 gold badges29 silver badges44 bronze badges
2
Also you can convert any number in any base to hex. Use this one line code here it's easy and simple to use:
hex[int[n,x]].replace["0x",""]
You have a string n
that is your number and x
the base of that number.
First, change it to integer and then to hex but hex has 0x
at the first of it so with replace
we remove it.
answered Dec 6, 2019 at 18:39
Prof.PlagueProf.Plague
4727 silver badges15 bronze badges
1
As an alternative representation you could use
[in] '%s' % hex[15]
[out]'0xf'
answered Jul 28, 2017 at 3:32
uzumakiuzumaki
1,48013 silver badges26 bronze badges