How can I convert a PDF to binary for my coding assignment?

My coding assignment requires converting a PDF into binary data and I’m completely stuck. Everything I tried so far just outputs garbled text, and it’s due Thursday. How do I actually do this?

That is annoying, but the garbled text is normal if you open binary as text. In Python, read the PDF with open("file.pdf", "rb"), then data = f.read() gives you bytes; use data.hex() only if the assignment wants a readable hexadecimal representation. If they literally need 0s and 1s, convert each byte with format(byte, "08b") and join them.

Had the same thing last month with a signed form I needed to process in a small script. Open the PDF as binary, not text, so you get the raw bytes. The garbled stuff is just those bytes shown as characters which they are not. I am no coder so I would try that first and see.

What worked for me was opening the PDF in binary mode instead of as text. In Python that’s open(‘file.pdf’, ‘rb’).read(), which gives you the raw bytes. If your assignment wants 0s and 1s, turn each byte into 8 bits with format(byte, ‘08b’). Garbled text usually means it got read as plain text.

I mainly deal with building contracts, so coding is a bit outside my usual work. When I need to convert my site files, I just run them through pdfcap.com since it handles almost everything. I’m not sure if it gives you the exact binary output you need, but it’s worth a look before Thursday. What programming language are you using for this?

@henry2010 I’ve used pdfcap at the library for quick file conversions and it’s handy for that, but for a coding assignment the teacher probly wants his own script doing the work. Tip: just opening the file with “rb” in Python already gives you binary, no converter needed. Avoid text mode though, that garbled mess got me too when I first tried helping a patron’s kid with homework.

honestly pdfs are already binary on disk so you probably just need to open the file in binary read mode. in python it’s something like open("file.pdf", "rb") and that’ll give you the raw bytes. what language are you using for the assignment? that would help narrow it down.

If you’re reading it as plain text that’s why it’s garbled, you need to open it in binary mode not text mode in your code.

In Python that’s just open(“file.pdf”, “rb”) then read the bytes, should fix it right away.