0

I'm calling a WCF service using HttpClient that returns a byte[] (pdf file) via rest/json. I cannot seem to be able the write the bytes correctly to a file on my pc because if I open the created file in notepad, it will just show the byte[] values (i.e. [23,44,21,etc]).

Any ideas?

byte[] responseResult = responseContent.ReadAsByteArrayAsync().Result;
File.WriteAllBytes(@"I:\document.pdf", responseResult);
6
  • Are you saying that when you write the byte array to a file, it shows the textual representation of the byte values (so "23" instead of 0x23)? Commented Jun 12, 2015 at 10:26
  • yes, I probably need to do some kind of conversion, but don't know what. Commented Jun 12, 2015 at 10:27
  • It sounds like the Result is actually a textual representation of the byte array, rather than actually containing an array of bytes. Have you had a look at the raw JSON to see what is in Result? Commented Jun 12, 2015 at 10:30
  • It may also be Base64 encoded text if it is indeed a string... so you can use msdn.microsoft.com/en-us/library/… Commented Jun 12, 2015 at 10:45
  • @MartinParkin I think that's the case, I now converted the byte[] to base64 string on the WCF side first, and that seems to work better. If you post it as answer, I'll mark it. Commented Jun 12, 2015 at 15:22

3 Answers 3

1

If you consider char 'a' then it internally stored as 97. Then we can say 'a' is 97. But 97 is different from string "97". "97" is considered as sequence 7067 when storing inside memory. So in memory its all bits. We separate bits to bytes. For our convenience we think of bytes in terms of integers. To do something useful we have to map these bytes to something that we interested in. 97->'a' is such that. That translation we call encoding. So what has happened is that you have got the raw bytes encoded.What you need is raw bytes. So responseContent is probably having the encoded text rather than raw bytes.

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

Comments

0

It sounds like the Result is actually a textual representation of the byte array, rather than actually containing an array of bytes.

Have you had a look at the raw JSON to see what is in Result?

If it's actually textual (either hexadecimal or base64) you will need to simply convert these to use them as a byte array.

Comments

0

use this method may be help you :

byte[] responseResult = System.Text.Encoding.ASCII.GetBytes("asjgdjasgdkjas"); File.WriteAllBytes(@"D:\mrig.txt", responseResult);

Comments

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.