2016-04-25 7 views
0

私はまたbNumが長いバイナリ文字列のpython3 Unicodeのデコードエラー

あるテキストはUTFからもともとあった場所binascii.unhexlify('%x' % (int('0b' + bNum, 2))).decode('utf-8')を使用しようとしたところX = b'\xe8\xd0\[email protected]\xee\xe4\xca\xc6\[email protected]\xde\[email protected]\xe8\xd0\[email protected]\xd0\xca\xe6\xe0\xca\xe4\xea\xe6\x14\xc4\[email protected]\xd0\xca\xdc\xe4\[email protected]\xee\xc2\xc8\xe6\xee\xde\xe4\xe8\[email protected]\xd8\xde\xdc\xce\xcc\xca\xd8\xd8\xde\xee\x14\x14\xd2\[email protected]\xee\xc2\[email protected]\xe8\xd0\[email protected]\xe6\xc6\xd0\xde\xde\xdc\xca\[email protected]\xd0\xca\xe6\xe0\xca\xe4\xea\xe6\[email protected]@@@@@\xe8\xd0\xc2\[email protected]\xe6\xc2\xd2\xd8\xca\[email protected]\xe8\xd0\[email protected]\xee\xd2\xdc\xe8\xe4\[email protected]\xe6\xca\xc2\x14\xc2\xdc\[email protected]\xe8\xd0\[email protected]\xe6\xd6\xd2\xe0\xe0\xca\[email protected]\xd0\xc2\[email protected]\xe8\xc2\xd6\xca\[email protected]\xd0\xd2\[email protected]\xd8\xd2\xe8\xe8\xd8\[email protected]\xc8\xc2\xea\xce\xd0\xe8\xca\xe4\[email protected]@@@@@\xe8\[email protected]\xc4\xca\xc2\[email protected]\xd0\xd2\[email protected]\xc6\xde\xda\xe0\xc2\xdc\xf2\\\x14\x14\xc4\xd8\xea\[email protected]\xee\xca\xe4\[email protected]\xd0\xca\[email protected]\xca\xf2\xca\[email protected]\xc2\[email protected]\xe8\xd0\[email protected]\xcc\xc2\xd2\xe4\xf2Z\xcc\xd8\xc2\xf0\[email protected]@@@@@\xd0\xca\[email protected]\xc6\xd0\xca\xca\xd6\[email protected]\xd8\xd2\xd6\[email protected]\xe8\xd0\[email protected]\xc8\xc2\xee\[email protected]\xde\[email protected]\xc8\xc2\xf2\x14\xc2\xdc\[email protected]\xd0\xca\[email protected]\xc4\xde\xe6\xde\[email protected]\xee\xd0\xd2\xe8\[email protected]\xc2\[email protected]\xe8\xd0\[email protected]\xd0\xc2\xee\xe8\xd0\xde\xe4\[email protected]\xc4\xea\xc8\xe6\[email protected]@@@@@\xe8\xd0\xc2\[email protected]\xde\xe0\[email protected]\xd2\[email protected]\xe8\xd0\[email protected]\xda\xde\xdc\xe8\[email protected]\xde\[email protected]\xda\xc2\xf2\\\x14\x14\xe8\xd0\[email protected]\xe6\xd6\xd2\xe0\xe0\xca\[email protected]\xd0\[email protected]\xe6\xe8\xde\xde\[email protected]\xc4\xca\xe6\xd2\xc8\[email protected]\xe8\xd0\[email protected]\xd0\xca\xd8\xda\[email protected]@@@@@\xd0\xd2\[email protected]\xe0\xd2\xe0\[email protected]\xee\xc2\[email protected]\xd2\[email protected]\xd0\xd2\[email protected]\xda\xde\xea\xe8\xd0\x14\xc2\xdc\[email protected]\xd0\[email protected]\xee\xc2\xe8\xc6\xd0\xca\[email protected]\xd0\xde\[email protected]\xe8\xd0\[email protected]\xec\xca\xca\xe4\xd2\xdc\[email protected]\xcc\xd8\xc2\[email protected]\xc8\xd2\[email protected]\xc4\xd8\xde\xee\[email protected]@@@@@\xe8\xd0\[email protected]\xe6\xda\xde\xd6\[email protected]\xdc\xde\[email protected]\xee\xca\xe6\[email protected]\xdc\xde\[email protected]\xe6\xde\xea\xe8\xd0\\\x14\x14\xe8\xd0\xca\[email protected]\xea\[email protected]\xc2\xdc\[email protected]\xe6\xe0\xc2\xd6\[email protected]\xc2\[email protected]\xde\xd8\[email protected]\xe6\xc2\xd2\xd8\xde\xe4\[email protected]@@@@@\xd0\xc2\[email protected]\xe6\xc2\xd2\xd8\xca\[email protected]\xe8\[email protected]\xe8\xd0\[email protected]\xe6\xe0\xc2\xdc\xd2\xe6\[email protected]\xda\xc2\xd2\xdc\x14\[email protected]\xe0\xe4\xc2\[email protected]\xe8\xd0\xca\[email protected]\xe0\xea\[email protected]\xd2\xdc\xe8\[email protected]\xf2\xde\xdc\xc8\xca\[email protected]\xe0\xde\xe4\xe8\[email protected]@@@@@\xcc\xde\[email protected]\[email protected]\xcc\xca\xc2\[email protected]\[email protected]\xd0\xea\xe4\xe4\xd2\xc6\xc2\xdc\xca\\\x14\x14\xd8\xc2\xe6\[email protected]\xdc\xd2\xce\xd0\[email protected]\xe8\xd0\[email protected]\xda\xde\xde\[email protected]\xd0\xc2\[email protected]\[email protected]\xce\xde\xd8\xc8\xca\[email protected]\xe4\xd2\xdc\xce\[email protected]@@@@@\xc2\xdc\[email protected]\xe8\xdeZ\xdc\xd2\xce\xd0\[email protected]\xdc\[email protected]\xda\xde\xde\[email protected]\xee\[email protected]\xe6\xca\xca\x14\xe8\xd0\[email protected]\xe6\xd6\xd2\xe0\xe0\xca\[email protected]\xd0\[email protected]\xc4'

codecs.decode(X, 'utf-8')を呼び出そうとするとき、私はUnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 0: invalid continuation byte

を取得-8エンコード.txtファイル

EDIT:2つのビット列があり、最初はテキストを変換する正確なビット列ですビット列。 2番目は画像から抽出されます。 2番目の画像は最初の画像と全く同じですが、隠されている画像には十分な画素がないため、画像が切り取られています。

例:彼らの両方が二番目はカットオフ点まで同じデータが含まれている場合http://pastebin.com/NnaH9dEb

なぜそれがUnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 0: invalid continuation byteエラーをスローしますか?

EDIT2:hex(int(<var name>, 2))で2ビットの文字列を16進数に変換すると、結果は異なりますが、最初の2バイトだけを変換すると同じ結果が返されます。

+0

私はソースがUTF-8でエンコードされたことを、あなたの仮定に挑戦するでしょう。 –

+0

@ MarkRansom前にメモ帳で++をチェックして再保存しましたが、もう一度やりましたが、私はまだ同じ問題があります。 –

+0

@ MarkRansomあなたが見てみたいのなら、ここに私のプログラムのコードがあります:http://pastebin.com/ZibMjms3イメージにテキストを隠します。それから、私はそれを取得しようとしているときにこのエラーが発生するので、おそらくそれは問題を引き起こして私の隠す機能です。この問題は、画像に挿入するテキストが画像のサイズを超えたときにのみ発生します(後でテキストを再構成するために各ビットを配置するのに十分なピクセルではありません)。ロシア語のような純粋なutf-8テキストではうまくいき、画像に収まらないものをカットしますが、通常の英字ではこのエラーになります。 –

答えて

2

decMsgのデコードがずれています。メッセージの最後に7個のゼロビットを追加するか、最後のビットを切り捨てると、私のメソッドでデコードされます。あなたのコードはTL; DRでした。

import math 

initMsg = '11101000110100001100101...' # truncated due post limits. 
decMsg = '11101000110100001100101...' 

# Only printing the first 25 chars of the message for bevity: 

a = int(initMsg,2) 
print(a.to_bytes(math.ceil(a.bit_length()/8),'big')[:25]) 

a = int(decMsg,2) 
print(a.to_bytes(math.ceil(a.bit_length()/8),'big')[:25]) 

a = int(decMsg+'0000000',2) 
print(a.to_bytes(math.ceil(a.bit_length()/8),'big')[:25]) 

a = int(decMsg[:-1],2) 
print(a.to_bytes(math.ceil(a.bit_length()/8),'big')[:25]) 

出力:

b'the wreck of the hesperus' 
b'\xe8\xd0\[email protected]\xee\xe4\xca\xc6\[email protected]\xde\[email protected]\xe8\xd0\[email protected]\xd0\xca\xe6\xe0\xca\xe4\xea\xe6' 
b'the wreck of the hesperus' 
b'the wreck of the hesperus' 

はバイナリでt\xe8の比較:

>>> format(ord('t'),'08b') 
'01110100' 
>>> format(0xe8,'08b') 
'11101000' 
+0

トンマークありがとう、これは本当に私のためにそれをクリアしました。 –

+0

@DarrelHolt related:[バイナリをASCIIに変換し、逆もまた同様に変換](http://stackoverflow.com/q/7396849/4279) – jfs

+0

@JFSebastianありがとうSebastian、私はすべてのUnicode文字のサポートについてあなたの記事を見ました。本当に良い見えます。 –