3

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);
16
  • duplicate of stackoverflow.com/questions/16072709/… Commented Feb 6, 2016 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. Commented Feb 6, 2016 at 15:39
  • what do you mean with 'I am able to open file'? Commented Feb 6, 2016 at 15:41
  • 1
    The \nthat 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. Commented Feb 6, 2016 at 15:47
  • 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. Commented Feb 6, 2016 at 16:41

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Short but could be more clearly expressed ;)
Using IRestResponse.RawBytes is really helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.