To decode base64 format you need to pass a bytes object to the base64.decodebytes function:
import base64
t = "AAADccB6OAAAAAAAAAAAAAAAAAAAAAAAAAA8WIEAAAAAAACxAAAAHgAACAAADAzBmAQwzoMABgCI AiTSSACCCAAhIAAAiAEMTMgMJibMsZuGeijn4BnI+YeQ0OMOKAACAgAKAABQAAQEABQAAAAAAAAA AA=="
t = t.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)
Answer from Jean-François Fabre on Stack OverflowTo decode base64 format you need to pass a bytes object to the base64.decodebytes function:
import base64
t = "AAADccB6OAAAAAAAAAAAAAAAAAAAAAAAAAA8WIEAAAAAAACxAAAAHgAACAAADAzBmAQwzoMABgCI AiTSSACCCAAhIAAAiAEMTMgMJibMsZuGeijn4BnI+YeQ0OMOKAACAgAKAABQAAQEABQAAAAAAAAA AA=="
t = t.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)
The easiest way to perform this action using Python 3 is this:
import base64
base64.b64decode(base64_to_binary_input)
io - How to decode base64 file into binary in Python? - Stack Overflow
Reddit Gold to Whoever Solve this issue - Converting Base64 Encoded into Binary Found in Python
python - Convert a base64 encoded string to binary - Stack Overflow
Python: How do I convert from binary to base 64 and back? - Stack Overflow
Hence the open's binary mode you have to use 'wb' else it gets saved as "text" basically.
import base64
# base64FileData <= the base64 file data
fileData = base64.urlsafe_b64decode(base64FileData.encode('UTF-8'))
with open('thefilename.pdf', 'wb') as theFile:
theFile.write(fileData)
Example your input data is came from this:
with open(local_image_path, "rb") as imageFile:
str_image_data = base64.b64encode(imageFile.read())
then to get the binary in variable you can try:
import io
import base64
binary_image_data = io.BytesIO(base64.decodebytes(str_image_data))
I am going nuts on this. We have a base64 encoded value:
"+G1mQl3A30MKQsCBxMXwyMOc/AZEvpcRRI13mUOy3SNDEFjyQuVQ0UKHFsdCJ/HCQqEvuEE5Lb5AAg=="
we needed decoded to binary. In python (databricks), we get this which is the correct answer (using
base64.b64decode):
"\xf8mfB]\xc0\xdfC\nB\xc0\x81\xc4\xc5\xf0\xc8\xc3\x9c\xfc\x06D\xbe\x97\x11D\x8dw\x99C\xb2\xdd#C\x10X\xf2B\xe5P\xd1B\x87\x16\xc7B'\xf1\xc2B\xa1/\xb8A9-\xbe@\x02"
However when using this:
SELECT CAST(N'' as xml).value('xs:base64Binary(sql:variable("@str"))', 'varbinary(400)')
I get this:
"0xF86D66425DC0DF430A42C081C4C5F0C8C39CFC0644BE9711448D779943B2DD23431058F242E550D1428716C74227F1C242A12FB841392DBE4002"
which is wrong wrong wrong.
I am pulling my hair out on this one. I will mow your lawn, give your reddit gold, name my third born child after you, you name it, if you can crack this. Thanks for any help in advance.
Depending on how you represent the value 0b100
>>> import struct
>>> val = 0b100
>>> print struct.pack('I', val).encode('base64')
BAAAAA==
This will turn your value into a 4-byte integer in native endianness, and encode that value into base64. You need to specify the width of your data as base64 is really made for representing binary data in printable characters.
You can typically encode data as a string of bytes via struct's functions, and then encode into base64. Ensure you're using the same data layout and endianess when decoding.
Binary literals are just normal python integers.
>>> 0b100
<<< 4
>>> 4
<<< 4
Just convert to a string and base64 encode it (giving you a string that is the base64 representation of '4'). There is nothing wrong with this approach, it's lossless and simple.
>>> s = str(0b100).encode('base64')
>>> int(s.decode('base64'))
<<< 4
If you want, you can use bin to convert the int into a binary string:
>>> bin(int(s.decode('base64')))
<<< '0b100'
If you have a Base64 string and you want to turn it back into an image, Python’s base64 library makes this just as easy.
Steps to Create base64 to image Converter in Python
Step 1: Import the Required Library
we will use the built-in base64 library, so make sure to import it:
import base64
Step 2: Get the Base64 String
You need a Base64 string that you want to convert back into an image. This could be one that you’ve stored or received from an API. Here’s a shortened example:
base64_string = "iVBORw0KGgoAAAANSUhEUgAAABAAAAA..."
Step 3: Decode the Base64 String
Once you have the Base64 string, use the base64.b64decode() function to convert it back into binary data.
Step 4: Write the Binary Data to an Image File
Now that you have the binary data, the final step is to save it as an image file. Use the open() function in "write binary" mode ('wb').
with open("output_image.png", "wb") as image_file:
image_file.write(image_data)Full Code Example for Converting Base64 to an Image
Here’s the complete Python code that converts a Base64 string back into an image:
import base64 # Step 1: Import the base64 library
# Step 2: Example Base64 string
base64_string = "iVBORw0KGgoAAAANSUhEUgAAABAAAAA..."
# Step 3: Decode the Base64 string back into binary data
image_data = base64.b64decode(base64_string)
# Step 4: Write the binary data to an image file
with open("output_image.png", "wb") as image_file:
image_file.write(image_data)Explanation:
-
base64.b64decode(): Decodes the Base64 string back into binary data. -
open("output_image.png", "wb"): Opens a new file in write-binary mode. -
image_file.write(): Writes the binary data into the file, creating the image.
just two lines could solve this problem:
import base64
base64_code = "VER30QHI30JFOIAIO3020085723F" # this is just an example
img_data = base64_code.encode()
content = base64.b64decode(img_data)
with open('/path/to/your/image.jpg', 'wb') as fw:
fw.write(content)
print(content)
then you could check the image bytes.
you can find the following here:
import base64
base64_message = 'UHl0aG9uIGlzIGZ1bg=='
base64_bytes = base64_message.encode('ascii')
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii')
print(message)
Source: https://stackabuse.com/encoding-and-decoding-base64-strings-in-python/
From the base64 package doc:
base64.encodestring:"Encode the bytes-like object s, which can contain arbitrary binary data, and return
bytescontaining the base64-encoded data, with newlines (b"\n") inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME)."
You want to use
base64.b64encode:"Encode the bytes-like object s using Base64 and return the encoded
bytes."
Example:
import base64
with open("test.zip", "rb") as f:
encodedZip = base64.b64encode(f.read())
print(encodedZip.decode())
The decode() will convert the binary string to text.
Use b64encode to encode without the newlines and then decode the resulting binary string with .decode('ascii') to get a normal string.
encodedZip = base64.b64encode(zipContents).decode('ascii')