0

I am trying to Encode one PDF using ASCII in C# and I am facing some problem. If PDF. ContentLenght increases like more that 1000000 then, I am unable to get encoded data in string but If I test in immediate window in VS2015 with same code I can see the encoded data but unable to get in string the same. Here the code which I am using

byte[] data = target.Array();               //PDF value in bytes
string data = Encoding.ASCII.GetString(data);  //encoded data in string 

This is what I get as a encoded data which is incorrect. can some one help me here.

 %PDF-1.7
    %????
    1 0 obj
    <</Type/Catalog/Pages 2 0 R/Lang(en-US) /StructTreeRoot 50 0 R/MarkInfo<</Marked true>>/Metadata 6566 0 R/ViewerPreferences 6567 0 R>>
    endobj
    2 0 obj
    <</Type/Pages/Count 20/Kids[ 4 0 R 12 0 R 14 0 R 16 0 R 18 0 R 20 0 R 22 0 R 24 0 R 26 0 R 28 0 R 30 0 R 32 0 R 34 0 R 36 0 R 38 0 R 40 0 R 42 0 R 44 0 R 46 0 R 48 0 R] >>
    endobj
    3 0 obj
    <</CreationDate(D:20200203070204+00'00') /ModDate(D:20200203070204+00'00') /Producer(??

Thanks in advance

  • Maybe you have to use Encoding.UTF8 or other encoding? Who generates the pdf? What encoding uses? – Liquid Core Feb 03 '20 at 08:11
  • But I am restricted to use only ASCII encoding, because End point is using ASCII decoding – pankash mann Feb 03 '20 at 08:16
  • Decode it with UTF8 or whatever, re encode it with ASCII when sending it back. – Liquid Core Feb 03 '20 at 08:16
  • PDFs are binary data. They're not plain text, and certainly not ASCII. If you absolutely need to represent the data *as* ASCII, you'll need to encode it, e.g. using base64. – Jon Skeet Feb 03 '20 at 08:18
  • Decoding and encoding it back will work, you think so ? – pankash mann Feb 03 '20 at 08:26
  • Yes, the whole point of base64 is that you can encode arbitrary binary data as text, and then decode it back to the original binary data. But both ends need to know about it. – Jon Skeet Feb 03 '20 at 08:29
  • There is no encoding in a binary file as such, you are thinking about this wrong. If you have bytes, they are bytes. As mentioned if you are transferring this to something that is expecting ASCII , you are probably wanting to base64 or something – TheGeneral Feb 03 '20 at 08:30
  • @JonSkeet can you please guide me with the flow. 1) convert bytes > Encoding ASCII 2 what will be the other steps – pankash mann Feb 03 '20 at 08:43
  • Encoding: `string text = Convert.ToBase64String(bytes);`. Decoding: `byte[] bytes = Convert.FromBase64String(text);`. It's as simple as that. – Jon Skeet Feb 03 '20 at 09:12
  • @JonSkeet just asking, since we're on the topic: Beside the incorrectness of treating data in a different format, would any kind of attempt to transfer a binary of any kind (pdf, exe, whatever) as an encoding which is not base64 work? IE passing a file between 2 endpoints ad an UTF8, ASCII or whatever? Why I can't pass the content of it as plain text in one of these encodings? Thanks – Liquid Core Feb 18 '20 at 15:13
  • @LiquidCore: Because it's *not* plain text. The byte sequence "0xf0 0xf0 0xf0" isn't a valid byte sequence in UTF-8, so which characters would you expect those to be transferred as in "plain text" that could then be translated *back* to "0xf0 0xf0 0xf0" again? – Jon Skeet Feb 18 '20 at 16:10
  • @JonSkeet ok, got it. Thank you. May I ask for some valid source for these kind of explanations? – Liquid Core Feb 19 '20 at 23:32
  • @LiquidCore: Um, I don't have a particular "source" for them - but I would expect that it's just common sense; if the bytes you're trying to *encode* can never be the result of *decoding* any text, then you're stuck. – Jon Skeet Feb 20 '20 at 06:43

0 Answers0