简体   繁体   中英

how to convert bytes to binary and return back the bytes

so I try to change the bytes to binary and get the bytes again. but when I check, the result I got was different. can someone fix it so I can get the same result?

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

i need li to count how many binary was there.

the password bytes i was input: b'QH5da48yXx7DGPdhRGPqIUHZOv7HXyPI7oKlaApNV3Y=' but what i get was 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='

i want get b'QH5da48yXx7DGPdhRGPqIUHZOv7HXyPI7oKlaApNV3Y=' again

the result i get from 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

the 0 was gone. how to keep the 0?

Just to clarify. I do not see where in your code its going so wrong?

Here is what I run to test:

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

As can be seen...the initial password is returnned by the very functions you use. So I really do not see what you are finding wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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