Python - 将十六进制转换为 INT/CHAR

发布于 2024-12-07 07:38:15 字数 662 浏览 1 评论 0原文

我在将十六进制更改为 int/char (最好是字符)时遇到一些困难。通过网站; http://home2.paulschou.net/tools/xlate/ 我输入 C0A80026 的十六进制到十六进制框中,在 DEC / CHAR 框中,它正确输出我期望它包含的 IP。

这些数据是从外部数据库中提取的,我不知道它是如何保存的,所以我所要做的就是十六进制字符串本身。

我尝试过使用 binascii.unhexlify 函数来查看是否可以解码它,但我担心我可能对十六进制没有足够的了解来欣赏我正在做的事情。

尝试仅使用 int() 转换进行打印也未产生所需的结果。我需要某种方法将十六进制字符串(或类似的字符串)转换为原始 IP。

更新:对于将来遇到此问题的任何人,我稍微修改了以下答案,以通过使用以下方式提供精确的打印输出作为 IP:

dec_output = str(int(hex_input[0:2], 16)) + "." +  str(int(hex_input[2:4], 16)) + "." + str(int(hex_input[4:6], 16)) + "." + str(int(hex_input[6:8], 16))

I am having some difficulty changing a hex to an int/char (char preferably). Via the website;
http://home2.paulschou.net/tools/xlate/ I enter the hex of C0A80026 into the hex box, in the DEC / CHAR box it correctly outputs the IP I expected it to contain.

This data is being pulled from an external database and I am not aware how it is being saved so all I have to work with is the hex string itself.

I have tried using the binascii.unhexlify function to see if I could decode it but I fear that I may not have a great enough understanding of hex to appreciate what I am doing.

Attemping to print just using an int() cast also has not produced the required results. I need some way to convert from that hex string (or one similar) to the original IP.

UPDATE: For anyone who comes across this in the future I modified the below answer slightly to provide an exact printout as an IP by using;

dec_output = str(int(hex_input[0:2], 16)) + "." +  str(int(hex_input[2:4], 16)) + "." + str(int(hex_input[4:6], 16)) + "." + str(int(hex_input[6:8], 16))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笑着哭最痛 2024-12-14 07:38:15

如果你想从中得到 4 个独立的数字,那么将其视为 4 个独立的数字。您不需要 binascii

hex_input  = 'C0A80026'
dec_output = [
    int(hex_input[0:2], 16), int(hex_input[2:4], 16),
    int(hex_input[4:6], 16), int(hex_input[6:8], 16),
]
print dec_output # [192, 168, 0, 38]

这可以概括,但我将其作为练习留给您。

If you want to get 4 separate numbers from this, then treat it as 4 separate numbers. You don't need binascii.

hex_input  = 'C0A80026'
dec_output = [
    int(hex_input[0:2], 16), int(hex_input[2:4], 16),
    int(hex_input[4:6], 16), int(hex_input[6:8], 16),
]
print dec_output # [192, 168, 0, 38]

This can be generalised, but I'll leave it as an exercise for you.

澉约 2024-12-14 07:38:15

这是一个简单的方法

>>> s = 'C0A80026'
>>> map(ord, s.decode('hex'))
[192, 168, 0, 38]
>>> 

如果您喜欢列表理解,

>>> [ord(c) for c in s.decode('hex')]
[192, 168, 0, 38]
>>> 

A simple way

>>> s = 'C0A80026'
>>> map(ord, s.decode('hex'))
[192, 168, 0, 38]
>>> 

if you prefer list comprehensions

>>> [ord(c) for c in s.decode('hex')]
[192, 168, 0, 38]
>>> 
dawn曙光 2024-12-14 07:38:15

您可能还需要 chr 函数:

chr(65) => 'A'

You might also need the chr function:

chr(65) => 'A'
肩上的翅膀 2024-12-14 07:38:15
>>> htext='C0A80026'
>>> [int(htext[i:i+2],16) for i in range(0,len(htext),2)]
# [192, 168, 0, 38]
>>> htext='C0A80026'
>>> [int(htext[i:i+2],16) for i in range(0,len(htext),2)]
# [192, 168, 0, 38]
神经暖 2024-12-14 07:38:15

要将十六进制字符串转换为人类可读的字符串,您可以像这样转义每个十六进制字符:

>>> '\x68\x65\x6c\x6c\x6f'
'hello'

从字符串您可以轻松循环到 INT 列表:

>>> hextoint = [ord(c) for c in '\x68\x65\x6c\x6c\x6f']
>>> _
[104, 101, 108, 108, 111]

您的示例:

>>> [ord(c) for c in '\xC0\xA8\x00\x26']
[192, 168, 0, 38]

For converting hex-string to human readable string you can escape every HEX character like this:

>>> '\x68\x65\x6c\x6c\x6f'
'hello'

from string you can easily loop to INT list:

>>> hextoint = [ord(c) for c in '\x68\x65\x6c\x6c\x6f']
>>> _
[104, 101, 108, 108, 111]

Your example:

>>> [ord(c) for c in '\xC0\xA8\x00\x26']
[192, 168, 0, 38]
人│生佛魔见 2024-12-14 07:38:15

我希望这是你所期望的:

hex_val = 0x42424242     # hexadecimal value
int_val = int(hex_val)   # integer value
str_val = str(int_val)   # string representation of integer value

I hope it's what you expect:

hex_val = 0x42424242     # hexadecimal value
int_val = int(hex_val)   # integer value
str_val = str(int_val)   # string representation of integer value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文