Pythonの24ビットストリームは正しい値を与えていませんか？

``````# 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'
``````

2

3 答え

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)))```

warvariuc の答えで指摘されているように、Pythonのstructモジュールは、さまざまな形式のレコードのパッキングとアンパックを管理するのに役立ちますが、Pythonの学習の現時点では、明示的なビットワイズ操作に固執する。

1

Looks like you're missing an & 0xff:

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

1

``````>>> 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'
>>>
``````
0