ebayuser_ladiesman217
ebayuser_ladiesman217

Reputation: 13

FromBase64String/UTF Encoding

My issue is based around a string of data that I'm getting back from an API call. I'm passing the raw data into FromBase64String and then encoding the byte array back to a string. I'm expecting a valid pdfsharp return that I'm saving to a file. None of the decoded string values below contain the correct data. I know the original base64 coded api return string is valid since I can open it in notepadd++ and use a base64 decoder to create the properly formatted pdf document.

byte[] todecode_byte = Convert.FromBase64String(data);
string decodedUTF7 = Encoding.UTF7.GetString(todecode_byte);
string decodedUTF8 = Encoding.UTF8.GetString(todecode_byte);

The closest representation to what I think it should be (the notepadd++ converted version) is the UTF7. But there seems to be some missing data within the embedded images within the document. UTF8 has some structural differences when comparing to the working document.

For example... My control... %PDF-1.7 %ÓôÌá 1 0 obj <<

UTF7... %PDF-1.7 %ÓôÌá 1 0 obj <<

UTF8... %PDF-1.7 %���� 1 0 obj <<

But, again, the UTF7 version seems to have issues revolving around the images that are embeded futher down in the document. Either way, both versions create an 88k pdf document that opens as a blank page. The control (using notepadd++), when saved as a pdf document, is about half of that size and will open displaying all of the correct information.

Upvotes: 1

Views: 685

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500505

I'm expecting a valid pdfsharp return that I'm saving to a file.

If it's meant to be a PDF file, I wouldn't try to convert that to a string at all. It's simply not text - it's binary data. It should be as simple as:

byte[] binaryData = Convert.FromBase64String(data);
File.WriteAllBytes("file.pdf", binaryData);

Upvotes: 2

Related Questions