'Python Decoding binary data back to file
i have a database in MSSQL with compressed and converted files, looks like this:
screenshot of values(every of them is 40k symbols long
i need to decode these files to pdf, docx and png files.
i've tried to do this via base64, but it didn't build correct files.
Do you have any ideas how could i decode all of them and build to correct file?
Solution 1:[1]
Your data appears to be a PNG with something pre-pended to the front of it. If you strip the first 12 bytes with dd and then revert the hex to binary with xxd you can recover the start of a PNG file:
dd bs=12 skip=1 if=YOURFILE | xxd -r -p > image.png
You can then check that PNG file and see its size and the fact that it is truncated like this:
pngcheck -v image.png
Sample Output
File: image.png (21833 bytes)
chunk IHDR at offset 0x0000c, length 13
2164 x 835 image, 24-bit RGB, non-interlaced
chunk sRGB at offset 0x00025, length 1
rendering intent = perceptual
chunk gAMA at offset 0x00032, length 4: 0.45455
chunk pHYs at offset 0x00042, length 9: 3779x3779 pixels/meter (96 dpi)
chunk IDAT at offset 0x00057, length 65445: EOF while reading data
ERRORS DETECTED in image.png
Solution 2:[2]
The data is hex-encoded, try:
from base64 import b16decode
# Data
encoded = '0x48656C6C6F'
decoded = b16decode(encoded[2:])
print(decoded)
Outputs b'Hello'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mark Setchell |
| Solution 2 | vaizki |
