Python 24 位流没有给出正确的值?

发布于 2024-12-15 07:49:35 字数 524 浏览 4 评论 0原文

我将 3 个字母转换为 ASCII 二进制表示法,然后将第一个字母增加 16 位,第二个字母增加 8 位,最后一个字母保持原样,这样当配置 24 位流时,前 8 位places 代表第一个字母,接下来的 8 个代表中间字母,最后一个代表最后一个字母。这是我的代码:

# create a block for the word 'Ozy'
bk1 = (ord('O')<<16) + (ord('z')<<8) + (ord('y'))
# Now take off the encryption for the block
cbk1 = ((chr(bk1>>16)) + (chr(bk1>>8)) + (chr(bk1&0xFF)))
# output of cbk1 is: 'O\u4f7ay'

这就是问题所在,第一个字母被解密为 O,最后一个字母和 y 一样正确,但由于某种原因它不会'不要为 z 做正确的事情。怎么了?

I am converting 3 letters into their ASCII binary notation and then incrementing the first letter by 16 places, the second letter by 8 places, and the last is staying where it is, so that when the 24 bit stream is configured, the first 8 bit places represents the first letter, the next 8 the middle letter and the last represent the last letter. Here's my code:

# create a block for the word 'Ozy'
bk1 = (ord('O')<<16) + (ord('z')<<8) + (ord('y'))
# Now take off the encryption for the block
cbk1 = ((chr(bk1>>16)) + (chr(bk1>>8)) + (chr(bk1&0xFF)))
# output of cbk1 is: 'O\u4f7ay'

So thats where the problem is, the first letter was decrypted as O, the last letter was correct as well as y, but for some reason it won't do the right thing for z. What's wrong?

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

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

发布评论

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

评论(3

尾戒 2024-12-22 07:49:35

看起来您缺少 & 0xff:

cbk1 = ((chr(bk1>>16)) + (chr((bk1>>8) & 0xff)) + (chr(bk1&0xFF)))

产生正确答案。当重新移回时,您还需要屏蔽高位,因为第一个字符设置的位(移位 16 位)仍然存在。

Looks like you're missing an & 0xff:

cbk1 = ((chr(bk1>>16)) + (chr((bk1>>8) & 0xff)) + (chr(bk1&0xFF)))

yields the correct answer. When reshifting back you need to mask out the upper bits as well because the bits set by the first character (shifted by 16 bit) will still be there.

泅人 2024-12-22 07:49:35

发生这种情况是因为您忘记过滤掉第一个字母中的位!
事实上,第二个字母“z”的 ASCII 值显示为“7a”,但正如您所看到的,它前面有“4f”(即“O”的 ASCII 值)。
尝试这样的事情:
cbk1 = ((chr(bk1>>16)) + (chr((bk1 & 0xFF00)>>8) ) + (chr(bk1&0xFF)))

如所指出的在 warvariuc 的回答中,Python 的 struct 模块有助于管理各种形式的记录的打包和解包,但此时当你学习 Python 和整个编码系统时,你可能想坚持使用显式的按位操作。

This happens because you forgot to filter-out the bits from the first letter!
Indeed the ASCII value for the 2nd letter, 'z', shows as '7a', but as you see it has '4f' (i.e. the ASCII for 'O'), in front of it.
Try something like:
cbk1 = ((chr(bk1>>16)) + (chr((bk1 & 0xFF00)>>8) ) + (chr(bk1&0xFF)))

As pointed out in warvariuc's answer, Python's struct module helps managing the packing and unpacking of records of various form, but at this point in your learning of Python and of codification systems at large, you probably want to stick with explicit bit-wise manipulation.

两个我 2024-12-22 07:49:35
>>> import struct
>>> a = chr(0) + 'Ozy' # make the data 4 byte long
>>> x = struct.unpack('>I', a)[0] # convert byte data into unsigned integer of 4 bytes
>>> hex(x) # it should be 3 bytes long, because first byte was 0x00
'0x4f7a79'
>>> a = struct.pack('>I', x)[1:] # pack the integer back to bytes and throw away the left most 0x00 byte
>>> a
'Ozy'
>>> 
>>> import struct
>>> a = chr(0) + 'Ozy' # make the data 4 byte long
>>> x = struct.unpack('>I', a)[0] # convert byte data into unsigned integer of 4 bytes
>>> hex(x) # it should be 3 bytes long, because first byte was 0x00
'0x4f7a79'
>>> a = struct.pack('>I', x)[1:] # pack the integer back to bytes and throw away the left most 0x00 byte
>>> a
'Ozy'
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文