関数の目的は、この要素を変更することです
image=[[(15,103,225), (0,3,19)],
[(22,200,1), (8,8,8)],
[(0,0,0), (5,123,19)]]
これにエンコード(ネストされた、メッセージ)
>>>encode(image,'hello)
[[(14, 103, 255), (0, 3, 18)], [(22, 200, 0), (9, 9, 8)], [(0, 1, 0), (5, 122, 19)]]
メッセージ内の各文字は対応する ASCII に変換され、次に各 ASCII は 8 桁の基数 2 の数値に変換されます。それをカバーしました。
今、上記のようにリストを変更するには、私が持っているものは次のとおりです。
def char_to_bits(char):
"""char_to_bits(char) -> string
Convert the input ASCII character to an 8 bit string of 1s and 0s.
>>> char_to_bits('A')
'01000001'
"""
result = ''
char_num = ord(char)
for index in range(8):
result = get_bit(char_num, index) + result
return result
def get_bit(int, position):
"""get_bit(int, position) -> bit
Return the bit (as a character, '1' or '0') from a given position
in a given integer (interpreted in base 2).
The least significant bit is at position 0. The second-least significant
bit is at position 1, and so forth.
>>> for pos in range(8):
... print(b.get_bit(167, pos))
...
1
1
1
0
0
1
0
1
"""
if int & (1 << position):
return '1'
else:
return '0'
def message_to_bits(message):
if len(message)==0:
return ''
for m in message:
return "".join("".join(str(bits.char_to_bits(m)))for m in message)
def set_bit_on(int, position):
"""set_bit_on(int, position) -> int
Set the bit at a given position in a given integer to 1,
regardless of its previous value, and return the new integer
as the result.
The least significant bit is at position 0. The second-least significant
bit is at position 1, and so forth.
>>> set_bit_on(0, 0)
1
>>> set_bit_on(0, 1)
2
>>> set_bit_on(167, 3)
175
"""
return int | (1 << position)
def set_bit_off(int, position):
"""set_bit_off(int, position) -> int
Set the bit at a given position in a given integer to 0,
regardless of its previous value, and return the new integer
as the result.
The least significant bit is at position 0. The second-least significant
bit is at position 1, and so forth.
>>> set_bit_off(0, 0)
0
>>> set_bit_off(1, 0)
0
>>> set_bit_off(167, 0)
166
>>> set_bit_off(175, 3)
167
"""
return int & ~(1 << position)
def set_bit(int, bit, position):
"""set_bit(int, bit, position) -> int
Set the bit at a given position to the given bit (as a char, either
'1' or '0') regardless of its previous value, and return the new integer
as the result.
The least significant bit is at position 0. The second-least significant
bit is at position 1, and so forth.
>>> set_bit(0, '1', 0)
1
>>> set_bit(0, '1', 1)
2
>>> set_bit(0, '1', 2)
4
>>> set_bit(0, '1', 3)
8
>>> set_bit(175, '0', 3)
167
>>> set_bit(175, '1', 3)
175
"""
if bit == '1':
return set_bit_on(int, position)
else:
return set_bit_off(int, position)
from collections import Iterable
def flatten(nested):
for item in nested:
if isinstance(item, Iterable) and not isinstance(item, basestring):
for x in flatten(item):
yield x
else:
yield item
def encode(nested, message):
position= 0
fnested= list(flatten(nested))
bitlist= list("".join("".join(str(bits.char_to_bits(m)))for m in message))
for int in fnested:
for bit in bitlist:
return "".join("".join(("".join(str(bits.set_bit(int,bit,position)))for int in fnested)) for bit in bitlist)
エンコードは長さ 1200 の文字列を返します。これは壮大な失敗であり、非常にユーザーフレンドリーではないように見えます。
助けてください