296

I want to take an integer (that will be <= 255), to a hex string representation

e.g.: I want to pass in 65 and get out '\x41', or 255 and get '\xff'.

I've tried doing this with the struct.pack('c',65), but that chokes on anything above 9 since it wants to take in a single character string.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
pynoob
  • 2,963
  • 2
  • 16
  • 4

14 Answers14

282

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.

Mike Graham
  • 73,987
  • 14
  • 101
  • 130
  • 2
    I thought chr converted integers to ascii. In which case chr(65) = 'A'. Is this a new addition? – diedthreetimes Nov 21 '12 at 20:17
  • 2
    @diedthreetimes, `chr` does not involve ASCII at all--it simply takes a number and makes a one-byte bytestring where the ordinal value of the byte is the number. ASCII and ASCII-compatible encodings come into play when you write and display bytestrings. – Mike Graham Nov 22 '12 at 14:08
  • 1
    @diedthreetimes, For example, they come in by making `'A'` another way to write and display `'\x41'`. All `str` really cares about is the fact this is sixty-five. To make things understandable and usable by humans, it often becomes an A. – Mike Graham Nov 22 '12 at 14:10
155

This will convert an integer to a 2 digit hex string with the 0x prefix:

strHex = "0x%0.2X" % integerVariable
Community
  • 1
  • 1
Greg Bray
  • 14,929
  • 12
  • 80
  • 104
  • 2
    I suggest to edit the code here and change it to this: strHex = "0x%0.2X" % integerVariable. (I wasn't able to edit myself.) – Samuel Apr 25 '17 at 16:49
91

What about hex()?

hex(255)  # 0xff

If you really want to have \ in front you can do:

print '\\' + hex(255)[1:]
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
79

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

monkut
  • 42,176
  • 24
  • 124
  • 155
  • For reference on this neat feature, PEP 498 -- Literal String Interpolation – MFT Sep 22 '18 at 04:05
  • 3
    IMHO, a bit more "readable" option can be '0x{:X}'.format(15) (notice X is in uppercase). For 2034 it will print 0x7F2 instead of 7f2 – Pavel Sapehin Mar 04 '20 at 04:53
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"

Dawid
  • 4,042
  • 2
  • 27
  • 30
29

For Python >= 3.6, use f-string formatting:

>>> x = 114514
>>> f'{x:0x}'
'1bf52'
>>> f'{x:#x}'
'0x1bf52'
Xinyi Li
  • 852
  • 8
  • 9
13

If you want to pack a struct with a value <255 (one byte unsigned, uint8_t) and end up with a string of one character, you're probably looking for the format B instead of c. C converts a character to a string (not too useful by itself) while B converts an integer.

struct.pack('B', 65)

(And yes, 65 is \x41, not \x65.)

The struct class will also conveniently handle endianness for communication or other uses.

XTL
  • 851
  • 1
  • 8
  • 23
12

With format(), as per format-examples, we can do:

>>> # format also supports binary numbers
>>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)
'int: 42;  hex: 2a;  oct: 52;  bin: 101010'
>>> # with 0x, 0o, or 0b as prefix:
>>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'
Map
  • 399
  • 4
  • 9
11

Note that for large values, hex() still works (some other answers don't):

x = hex(349593196107334030177678842158399357)
print(x)

Python 2: 0x4354467b746f6f5f736d616c6c3f7dL
Python 3: 0x4354467b746f6f5f736d616c6c3f7d

For a decrypted RSA message, one could do the following:

import binascii

hexadecimals = hex(349593196107334030177678842158399357)

print(binascii.unhexlify(hexadecimals[2:-1])) # python 2
print(binascii.unhexlify(hexadecimals[2:])) # python 3
Luc
  • 5,339
  • 2
  • 48
  • 48
  • Well... no. I tried `hex(hash(text))` and it produced a negative integer which resulted in a hex string with minus sign. Who did implement this? – JPT Apr 05 '21 at 13:14
  • @JPT I don't understand your question. If the value is negative, then obviously the hex value will have a minus sign. Why would that be different when writing in hexadecimal, binary, decimal, or any other number system? Or are you looking for the in-memory representation, where there is a bit that determines the sign by being set or unset? – Luc Apr 05 '21 at 13:17
  • 2
    Well, the computer scientist is expecting negative hex to be something starting with a sign flag, so for example FFFx xxxx or 8000 xxxx, not an actual sign. I've never ever seen a hex with a minus sign before. But if you put it like that... ;) – JPT Apr 06 '21 at 14:03
  • @JPT the hext values you usually see are just a hex representation of a binary value. It is a matter of coding (Two's complement) that it is used in computer science. In the past different way of representing negative binary numbers were also used but Two's complement has biggest advantages. – Filip May 18 '23 at 09:44
  • @Filip exactly, that's why a HEX with minus sign is not an exact representation. It's an interpretation. – JPT May 19 '23 at 12:05
10
(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'
8

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
shakram02
  • 10,812
  • 4
  • 22
  • 21
7

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.

Prof.Plague
  • 649
  • 10
  • 20
4

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)
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
ncmathsadist
  • 4,684
  • 3
  • 29
  • 45
-1

As an alternative representation you could use

[in] '%s' % hex(15)
[out]'0xf'
uzumaki
  • 1,743
  • 17
  • 32