如何将字节转换为二进制并返回字节



所以我尝试将字节更改为二进制并再次获得字节。但当我检查时,我得到的结果是不同的。有人能修好它,这样我就能得到同样的结果吗?

def bytestobiner(password):
print(password)
li = []
for my_byte in password:
if my_byte != None:
# string_output = ' '.join(f'{my_byte:0>8b}' for my_byte in password)
string_output = ' '.join('{:08b}'.format(x) for x in bytearray(password))
li.append(string_output)

return li, len(string_output.split(' '))
def binertobytes(f):
print("biner bytes")
print(f)
hasil = bytes([int(f[i:i+8], 2) for i in range(0, len(f), 8)])
return hasil

我需要li来计算有多少个二进制文件。

输入的密码字节i:b'QH5da48yXx7DGPdhRGPqIUHZOv7HXyPI7oKlaApNV3Y='但我得到的是b'xa2x90jxc8xc2hpxf2xb0xf0nx88x8exa0xc8xd0xa4x8exa0xe2x92xaax90xb4x9execnx90xb0xf2xa0x92nxdex96xd8xc2x82xe0x9cxacfxb2='

i want to getb'QH5da48yXx7DGPdhRGPqIUHZOv7HXyPI7oKlaApNV3Y='again

我从bytestbinary得到的结果:

01010001 01001000 00110101 01100100 01100001 00110100 00111000 01111001 01011000 01111000 00110111 01000100 01000111 01010000 01100100 01101000 01010010 01000111 01010000 01110001 01001001 01010101 01001000 01011010 01001111 01110110 00110111 01001000 01011000 01111001 01010000 01001001 00110111 01101111 01001011 01101100 01100001 01000001 01110000 01001110 01010110 00110011 01011001 00111101

和从binarytobytes得到的f:101000101001000001101010110010001100001001101000011100001111001010110000111100000110111010001000100011101010000011001000110100001010010010001110101000001110001010010010101010101001000010110100100111101110110001101110100100001011000011110010101000001001001001101110110111101001011011011000110000101000001011100000100111001010110001100110101100100111101

0消失了。如何保持0?

澄清一下。我看不出你的代码哪里出错了?

下面是我要测试的代码:

def bytestobiner(password):
print(password)
li = []
for my_byte in password:
if my_byte != None:
# string_output = ' '.join(f'{my_byte:0>8b}' for my_byte in password)
string_output = ' '.join('{:08b}'.format(x) for x in bytearray(password))
li.append(string_output)

return li, len(string_output.split(' '))
def binertobytes(f):
print("biner bytes")
print(f)
hasil = bytes([int(f[i:i+8], 2) for i in range(0, len(f), 8)])
return hasil
initial_pass = b'QH5da48yXx7DGPdhRGPqIUHZOv7HXyPI7oKlaApNV3Y='
val = bytestobiner(initial_pass)
val 
--> (['01010001 01001000 00110101 01100100 01100001 00110100 00111000 01111001 01011000 01111000 00110111 01000100 01000111 01010000 01100100 01101000 01010010 01000111 01010000 01110001 01001001 01010101 01001000 01011010 01001111 01110110 00110111 01001000 01011000 01111001 01010000 01001001 00110111 01101111 01001011 01101100 01100001 01000001 01110000 01001110 01010110 00110011 01011001 00111101'], 44)
reformatted_val = val[0][0].replace(' ','')
returned_pass = binertobytes(reformatted_val)
returned_pass 
--> b'QH5da48yXx7DGPdhRGPqIUHZOv7HXyPI7oKlaApNV3Y='
returned_pass == initial_pass
--> True

可以看出……初始密码由您使用的函数返回。所以我真的不明白你找错了什么。

相关内容

  • 没有找到相关文章

最新更新