字节和整数以及串联和 python

发布于 2024-10-10 00:05:11 字数 398 浏览 3 评论 0原文

我有 2 个 32 位无符号整数..

777007543 和 114997259

和字节串..

0x47 0x30 0x22 0x2D 0x5A 0x3F 0x47 0x58

我如何让python给我这3个的串联,这样我就有...

0x2E 0x50 0x31 0xB7 0x06 0xDA 0xB8 0x0B 0x47 0x30 0x22 0x2D 0x5A 0x3F 0x47 0x58

然后我会通过md5哈希运行它并得到...

0x30 0x73 0x74 0x33 0x52 0x6C 0x26 0x71 0x2D 0x32 0x5A 0x55 0x5E 0x77 0x65 0x75

如果有人可以在python代码中运行它,我将不胜感激

I have 2 32bit unsigned integers..

777007543
and
114997259

and the string of bytes..

0x47 0x30 0x22 0x2D 0x5A 0x3F 0x47 0x58

How do I get python to give me the concatenation of these 3 such that I have...

0x2E 0x50 0x31 0xB7 0x06 0xDA 0xB8 0x0B 0x47 0x30 0x22 0x2D 0x5A 0x3F 0x47 0x58

I would then run that through an md5 hash and get...

0x30 0x73 0x74 0x33 0x52 0x6C 0x26 0x71 0x2D 0x32 0x5A 0x55 0x5E 0x77 0x65 0x75

If anyone could run that through in python code it would be much appreciated

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

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

发布评论

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

评论(3

时光暖心i 2024-10-17 00:05:11
import struct
import hashlib

x = struct.pack('>II8B', 777007543, 114997259, 0x47, 0x30, 0x22, 0x2D, 0x5A, 0x3F, 0x47, 0x58)
hash = hashlib.md5(x).digest()

print [hex(ord(d)) for d in x]
(output) ['0x2e', '0x50', '0x31', '0xb7', '0x6', '0xda', '0xb8', '0xb', '0x47', '0x30', '0x22', '0x2d', '0x5a', '0x3f', '0x47', '0x58']

print [hex(ord(d)) for d in hash]
(output) ['0x30', '0x73', '0x74', '0x33', '0x52', '0x6c', '0x26', '0x71', '0x2d', '0x32', '0x5a', '0x55', '0x5e', '0x77', '0x65', '0x75']
import struct
import hashlib

x = struct.pack('>II8B', 777007543, 114997259, 0x47, 0x30, 0x22, 0x2D, 0x5A, 0x3F, 0x47, 0x58)
hash = hashlib.md5(x).digest()

print [hex(ord(d)) for d in x]
(output) ['0x2e', '0x50', '0x31', '0xb7', '0x6', '0xda', '0xb8', '0xb', '0x47', '0x30', '0x22', '0x2d', '0x5a', '0x3f', '0x47', '0x58']

print [hex(ord(d)) for d in hash]
(output) ['0x30', '0x73', '0x74', '0x33', '0x52', '0x6c', '0x26', '0x71', '0x2d', '0x32', '0x5a', '0x55', '0x5e', '0x77', '0x65', '0x75']
感性不性感 2024-10-17 00:05:11
q = hex(777007543) + hex(114997259)[2:] + '4730222d5a3f4758'

就这样做。这就是它起作用的原因:

>>> num1, num2
(777007543, 114997259)
>>> hex(num1), hex(num2)
('0x2e5031b7', '0x6dab80b')
>>> hex(num1) + hex(num2) + '0x4730222d5a3f4758'
'0x2e5031b70x6dab80b0x4730222d5a3f4758'
>>> hex(num1) + hex(num2)[2:] + '4730222d5a3f4758'
'0x2e5031b76dab80b4730222d5a3f4758'
>>> int(_, 16)
3847554995347152223960862296285071192L

但是,如果您想要编辑,准确处理您在答案中显示的表示并不困难

这是斯科特·格里菲茨所说的。他是对的;)

"

使用十六进制仅适用于此,因为
数字足够大,需要 8 个十六进制
数字。我们需要使用一种格式,用于
示例 '{0:08x}{1:08x}'.format(num1,
num2)
将用最多填充十六进制
八个零。

"

q = hex(777007543) + hex(114997259)[2:] + '4730222d5a3f4758'

just do this. here's why it works:

>>> num1, num2
(777007543, 114997259)
>>> hex(num1), hex(num2)
('0x2e5031b7', '0x6dab80b')
>>> hex(num1) + hex(num2) + '0x4730222d5a3f4758'
'0x2e5031b70x6dab80b0x4730222d5a3f4758'
>>> hex(num1) + hex(num2)[2:] + '4730222d5a3f4758'
'0x2e5031b76dab80b4730222d5a3f4758'
>>> int(_, 16)
3847554995347152223960862296285071192L

it's not difficult however to deal exactly with the representation you showed in your answer, if you want

edit:

here is what scott griffhits said. He's right ;)

"

Using hex only works here because the
numbers are large enough to need 8 hex
digits. We need to use a format, for
example '{0:08x}{1:08x}'.format(num1,
num2)
will pad the hex with up to
eight zeros.

"

缱倦旧时光 2024-10-17 00:05:11

这将为您提供所需的所有值的列表,

>>> [777007543 >> i & 0xff for i in xrange(24,0,-8)] + \
... [114997259 >> i & 0xff for i in xrange(24,0,-8)] + \
... map(ord, stringofbytes)

甚至更好(来自 您启动的其他线程),

>>> struct.unpack('>12B', \
... struct.pack('>L', 777007543) + struct.pack('>L', 114997259) + '.P1\xb7')

如果您想将其作为字符串传递给您的 md5 散列,

>>> map(chr, _)

我假设字符串的每个字节应该代表一个1字节的数字。

This will give you a list of all the values you want,

>>> [777007543 >> i & 0xff for i in xrange(24,0,-8)] + \
... [114997259 >> i & 0xff for i in xrange(24,0,-8)] + \
... map(ord, stringofbytes)

or even better (from the other thread you started),

>>> struct.unpack('>12B', \
... struct.pack('>L', 777007543) + struct.pack('>L', 114997259) + '.P1\xb7')

If you then want to make this a string to pass to your md5 hash,

>>> map(chr, _)

I am assuming that each byte of the string is supposed to represent a 1byte number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文