Int to hex string python

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.

Int to hex string python

BartoszKP

33.8k13 gold badges103 silver badges128 bronze badges

asked Feb 16, 2010 at 0:07

0

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.

answered Feb 16, 2010 at 0:10

Int to hex string python

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

Int to hex string python

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

Int to hex string python

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: http://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 <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.

answered Sep 19, 2011 at 12:11

Int to hex string python

XTLXTL

8321 gold badge8 silver badges23 bronze badges

1

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'

answered Sep 5, 2018 at 1:28

MapMap

3694 silver badges9 bronze badges

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

answered Oct 5, 2016 at 21:29

LucLuc

4,6132 gold badges43 silver badges44 bronze badges

3

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

>>> x = 114514
>>> f'{x:0x}'
'1bf52'
>>> f'{x:#x}'
'0x1bf52'

answered Apr 5 at 4:57

Int to hex string python

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

Int to hex string python

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)

Int to hex string python

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

Int to hex string python

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

How will you convert an integer to a hexadecimal string in Python?

hex() function in Python. hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.

How do you create a hex string in Python?

To convert Python String to hex, use the inbuilt hex() method. The hex() is a built-in method that converts the integer to a corresponding hexadecimal string. For example, use the int(x, base) function with 16 to convert a string to an integer.

Which method converts an integer to a hexadecimal string?

An integer can be converted to a hexadecimal by using the string. ToString() extension method. int. Parse − Converts the string representation of a number to its 32-bit signed integer equivalent.

What is Python base16?

In base 16 (also called "hexadecimal" or "hex" for short) you start at 0 then count up 0123456789ABCDEF (16 digits in total). The int function accepts any number from 2 and 36 as the base, it just extends the alphabet: base 36 is 0123456789ABCEDFGHIJKLMNOPQRSTUVWXYZ .