Python converting from base64 to binary

前端 未结 2 1463
挽巷
挽巷 2021-01-11 09:10

I have a problem about converting a base64 encoded string into binary. I am collecting the Fingerprint2D in the following link,

url = \"https://pubchem.ncbi.         


        
相关标签:
2条回答
  • 2021-01-11 09:54

    To decode base64 format you need to pass a bytes object to the base64.decodebytes function:

    import base64
    
    t = "AAADccB6OAAAAAAAAAAAAAAAAAAAAAAAAAA8WIEAAAAAAACxAAAAHgAACAAADAzBmAQwzoMABgCI AiTSSACCCAAhIAAAiAEMTMgMJibMsZuGeijn4BnI+YeQ0OMOKAACAgAKAABQAAQEABQAAAAAAAAA AA==".encode("ascii")
    
    decoded = base64.decodebytes(t)
    
    print(decoded)
    print(len(decoded)*8)
    

    I get the following:

    b'\x00\x00\x03q\xc0z8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00<X\x81\x00\x00\x00\x00\x00\x00\xb1\x00\x00\x00\x1e\x00\x00\x08\x00\x00\x0c\x0c\xc1\x98\x040\xce\x83\x00\x06\x00\x88\x02$\xd2H\x00\x82\x08\x00! \x00\x00\x88\x01\x0cL\xc8\x0c&&\xcc\xb1\x9b\x86z(\xe7\xe0\x19\xc8\xf9\x87\x90\xd0\xe3\x0e(\x00\x02\x02\x00\n\x00\x00P\x00\x04\x04\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00'
    920
    

    So 920 bits as expected.

    To get data as binary just iterate on the bytes and convert to binary using format and zero-padding to 8 digits (bin adds a 0b header so it's not suitable), and join the strings together:

    print("".join(["{:08b}".format(x) for x in decoded]))
    

    results in:

    00000000000000000000001101110001110000000111101000111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011110001011000100000010000000000000000000000000000000000000000000000001011000100000000000000000000000000011110000000000000000000001000000000000000000000001100000011001100000110011000000001000011000011001110100000110000000000000110000000001000100000000010001001001101001001001000000000001000001000001000000000000010000100100000000000000000000010001000000000010000110001001100110010000000110000100110001001101100110010110001100110111000011001111010001010001110011111100000000110011100100011111001100001111001000011010000111000110000111000101000000000000000001000000010000000000000101000000000000000000101000000000000000001000000010000000000000101000000000000000000000000000000000000000000000000000000000000000000
    

    (which is 920 chars, as expected)

    0 讨论(0)
  • 2021-01-11 10:02

    the easiest way with python 3 is:

    import base64    
    base64.b64decode(base64_to_binary_input) 
    
    0 讨论(0)
提交回复
热议问题