繁体   English   中英

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

[英]how to convert bytes to binary and return back the bytes

所以我尝试将字节更改为二进制并再次获取字节。 但是当我检查时,我得到的结果是不同的。 有人可以修复它以便我得到相同的结果吗?

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 来计算那里有多少个二进制文件。

我输入的密码字节: b'QH5da48yXx7DGPdhRGPqIUHZOv7HXyPI7oKlaApNV3Y='但我得到的是b'\xa2\x90j\xc8\xc2hp\xf2\xb0\xf0n\x88\x8e\xa0\xc8\xd0\xa4\x8e\xa0\xe2\x92\xaa\x90\xb4\x9e\xecn\x90\xb0\xf2\xa0\x92n\xde\x96\xd8\xc2\x82\xe0\x9c\xacf\xb2='

我想再次获得b'QH5da48yXx7DGPdhRGPqIUHZOv7HXyPI7oKlaApNV3Y='

我从 bytestobinary 得到的结果:

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

and the fi get from binarytobytes: 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

可以看出...初始密码是由您使用的函数返回的。 所以我真的看不出你发现了什么错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM