2

I am trying to develop an application in C# which takes data from Service1(3rd party), processes it and then sends data to Service2(again 3rd party).

The data I am trying to receive, process and send is a pdf file

From Service1, I am receiving pdf file in a string variable.

e.g.

response.Content = "%PDF-1.4 \n1 0 obj\n<<\n/Pages 2 0 R\n/Type /Catalog\n>>\nendobj\n2 0 obj\n<<\n/Type /Pages\n/Kids [ 3 0 R 17 0 R ]\n/Count 2\n>>\nendobj\n3 0 obj\n<<\n/Type /Page\n/Parent 2 0 R\n/Resources <<\n/XObject << /Im0 8 0 R >>\n/ProcSet 6 0 R
>>\n/MediaBox [0 0 ..."

Now, Service2 requires PDF data to be in byte[] form

How do I convert response.Content i.e. string to byte[]?

FYI -

I have tried below method, but it didn't work. I am able to open file but it shows all junk values.

byte[] bitmapData = Encoding.UTF8.GetBytes(response.Content);
TylerH
  • 20,799
  • 66
  • 75
  • 101
Amol Agre
  • 59
  • 1
  • 7
  • duplicate of http://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp – Cecilio Pardo Feb 06 '16 at 15:31
  • Well, I've already looked into that post and tried different ways too, but still I'm not getting data in proper format. – Amol Agre Feb 06 '16 at 15:39
  • what do you mean with 'I am able to open file'? – Cecilio Pardo Feb 06 '16 at 15:41
  • I mean earlier I was using byte[] bitmapData = Encoding.ASCII.GetBytes(response.Content); .. after which I tried to open pdf file, but pdf reader said 'file is corrupted' or something like that – Amol Agre Feb 06 '16 at 15:45
  • 1
    The `\n`that are on the PDF string are literally like that? If that is the case, then the string contains scaped (modified) characters. You will have to replace \n with newlines. – Cecilio Pardo Feb 06 '16 at 15:47
  • _From Service1, I am receiving pdf file in a string variable._ - This is a problem. In c# a string contain unicode characters. A pdf file contains bytes. – wimh Feb 06 '16 at 15:48
  • Yes. That's how it exactly is as I seen in debug mode. And how do I replace \n with newlines? I didn't get that.. isn't both same? – Amol Agre Feb 06 '16 at 15:50
  • Yes Wimmel.. That's where I thought problem is.. But since Service1 being from 3rd party reputed developer, I scrapped that thought. – Amol Agre Feb 06 '16 at 15:53
  • If you open the file with a text editor, what do you see? – Cecilio Pardo Feb 06 '16 at 15:53
  • Something like this - %PDF-1.4 1 0 obj << /Pages 2 0 R /Type /Catalog >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R 17 0 R ] /Count 2 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /XObject << /Im0 8 0 R >> /ProcSet 6 0 R >> /MediaBox [0 0 – Amol Agre Feb 06 '16 at 15:56
  • That looks ok (assuming you have multiple lines, not just one). Hard to tell what is going on. If you could get the original PDF file and test for differences, it would probably be clear. Sorry. – Cecilio Pardo Feb 06 '16 at 15:59
  • I actually don't have Source/Original file.. Just know that Service1 sends pdf data in that format. Nvm, thanks. – Amol Agre Feb 06 '16 at 16:01
  • I agree with @Wimmel: as soon as you have a Pdf as a string, you quite likely have damaged it. Can you not access the web service 1 response as raw bytes? – mkl Feb 06 '16 at 16:29
  • 1
    Yes mkl.. was just looking into it again and saw that.. Actually I was using _RestSharp_ for my call to service and I was referring to string _Content_ variable from response. Just saw that there is another variable _RawBytes_ which is byte[].. Anyways thanks again all of you. I'll update my post. – Amol Agre Feb 06 '16 at 16:41
  • As that resolves the issue, you should make that an actual answer, not an appendix of your question, and accept that answer as soon as you are allowed to (As fast As I know you have to wait some hours before you can accept an own answer). – mkl Feb 06 '16 at 20:59
  • Thanks, IRestResponse.RawBytes works fine :) – Amit K Khanchandani Oct 06 '16 at 05:56

1 Answers1

3

I just found that it was not Service1 which was sending data in string format, it's just I was using IRestResponse.Content, a string variable from RestSharp instead of using IRestResponse.RawBytes, a byte[] variable.

Amol Agre
  • 59
  • 1
  • 7