如何将int转换为十六进制字符串?

时间:2021-06-06 15:45:30

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

我要取一个整数(即<= 255),以十六进制字符串表示形式。

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

例如:我想通过65,得到'\x41',或者255,得到'\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.

我已经尝试过使用struct.pack('c',65),但它会在任何大于9的地方阻塞,因为它想接收单个字符串。

10 个解决方案

#1


159  

You are looking for the chr function.

你在寻找chr函数。

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.

注意,这与包含整数为十六进制的字符串非常不同。如果这是你想要的,使用十六进制。

#2


98  

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

这将把一个整数转换为一个有0x前缀的2位十六进制字符串:

strHex = "0x%0.2X" % 255

#3


40  

What about hex()?

十六进制()呢?

hex(255)  # 0xff

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

如果你真的想要拥有你可以做的:

print '\\' + hex(255)[1:]

#4


37  

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"

Python文档说:“把这个放在枕头下面:http://docs.python.org/library/index.html”

#5


10  

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价值< 255(一个字节无符号,uint8_t)和最后一个字符的字符串,你可能会寻找B代替c。c格式将字符转换为一个字符串(不太有用的本身),而B转换为一个整数。

struct.pack('B', 65)

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

(是的,65是\x41,不是\x65。)

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

struct类还可以方便地处理通信或其他用途的endianness。

#6


8  

Let me add this one, because sometimes you just want the single digit representation:

让我加上这个,因为有时候你只需要一个数字表示:

'{:x}'.format(15)
> f

And now with the new f'' format strings you can do:

现在有了新的f格式字符串,你可以做:

f'{15:x}'
> f

NOTE: the initial 'f' in f'{15:x}' is to signify a format string

注意:f'{15:x}中的首字母“f”表示格式字符串

#7


4  

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

注意,对于较大的值,十六进制()仍然有效(其他一些答案没有):

x = hex(349593196107334030177678842158399357)
print(x)

Python 2: 0x4354467b746f6f5f736d616c6c3f7dL
Python 3: 0x4354467b746f6f5f736d616c6c3f7d

Python 2: 0x4354467b746f6f5f736d616c6c3f7dL Python 3: 0x4354467b746f5f736d6c6c3f7d

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

对于解密的RSA消息,可以执行以下操作:

import binascii

hexadecimals = hex(349593196107334030177678842158399357)

print(binascii.unhexlify(hexadecimals[2:-1])) # python 2
print(binascii.unhexlify(hexadecimals[2:])) # python 3

#8


3  

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)

#9


2  

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

改变(2)如果你想要一个更大的宽度的数字(2是2个十六进制字符),那么3会给你以下信息

"0x%03X" % 5  # => 0x005
"0x%03X" % 17 # => 0x011

#10


0  

As an alternative representation you could use

作为一种可选的表示形式

[in] '%s' % hex(15)
[out]'0xf'

#1


159  

You are looking for the chr function.

你在寻找chr函数。

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.

注意,这与包含整数为十六进制的字符串非常不同。如果这是你想要的,使用十六进制。

#2


98  

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

这将把一个整数转换为一个有0x前缀的2位十六进制字符串:

strHex = "0x%0.2X" % 255

#3


40  

What about hex()?

十六进制()呢?

hex(255)  # 0xff

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

如果你真的想要拥有你可以做的:

print '\\' + hex(255)[1:]

#4


37  

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"

Python文档说:“把这个放在枕头下面:http://docs.python.org/library/index.html”

#5


10  

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价值< 255(一个字节无符号,uint8_t)和最后一个字符的字符串,你可能会寻找B代替c。c格式将字符转换为一个字符串(不太有用的本身),而B转换为一个整数。

struct.pack('B', 65)

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

(是的,65是\x41,不是\x65。)

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

struct类还可以方便地处理通信或其他用途的endianness。

#6


8  

Let me add this one, because sometimes you just want the single digit representation:

让我加上这个,因为有时候你只需要一个数字表示:

'{:x}'.format(15)
> f

And now with the new f'' format strings you can do:

现在有了新的f格式字符串,你可以做:

f'{15:x}'
> f

NOTE: the initial 'f' in f'{15:x}' is to signify a format string

注意:f'{15:x}中的首字母“f”表示格式字符串

#7


4  

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

注意,对于较大的值,十六进制()仍然有效(其他一些答案没有):

x = hex(349593196107334030177678842158399357)
print(x)

Python 2: 0x4354467b746f6f5f736d616c6c3f7dL
Python 3: 0x4354467b746f6f5f736d616c6c3f7d

Python 2: 0x4354467b746f6f5f736d616c6c3f7dL Python 3: 0x4354467b746f5f736d6c6c3f7d

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

对于解密的RSA消息,可以执行以下操作:

import binascii

hexadecimals = hex(349593196107334030177678842158399357)

print(binascii.unhexlify(hexadecimals[2:-1])) # python 2
print(binascii.unhexlify(hexadecimals[2:])) # python 3

#8


3  

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)

#9


2  

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

改变(2)如果你想要一个更大的宽度的数字(2是2个十六进制字符),那么3会给你以下信息

"0x%03X" % 5  # => 0x005
"0x%03X" % 17 # => 0x011

#10


0  

As an alternative representation you could use

作为一种可选的表示形式

[in] '%s' % hex(15)
[out]'0xf'