0

I have a webservice that returns a binary data as a string. Using C# code how can I store it in byte array? Is this the right way?

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(inputString);

Actually, this didn't work. Let me explain it more: the web service code converts a string (containing XSLFO data) into byte array using utf8 encoding. In my web service response I only see data something like "PGZvOnJvb3QgeG1sbnM6Zm89Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML0Zvcm1hdCIgeG1sbnM­6eGY9Imh0dHA6Ly93d3cuZWNyaW9uLmNvbS94Zi8xLjAiIHhtbG5zOm1zeHNsPSJ1c==". Actually I would like to have the original string value that was converted into byte[] in the service. Not sure if it possible?

3
  • 1
    yes, the code you posted is correct, assuming that the data is encoded using UTF8. Commented May 6, 2011 at 18:27
  • 1
    What format is the binary data in? Is it in Base64, UTF8 or what? The encoding matters. Commented May 6, 2011 at 18:28
  • 1
    @user465876: You said it was originally binary data... what kind? Treating (say) a picture as a UTF-8-encoded string is a bad idea. Commented May 6, 2011 at 18:36

2 Answers 2

12

No, that's a bad idea.

Unless the input data was originally text, trying to use Encoding is a bad idea. The web service should be using something like base64 to encode it - at which point you can use Convert.FromBase64String to get the original binary data back.

Basically, treating arbitrary binary data as if it were encoded text is a quick way to lose data. When you need to represent binary data in a string, you should use base64, hex or something similar.

This may mean you need to change the web service as well, of course - if it's creating the string by simply treating the binary data as UTF-8-encoded text, it's broken to start with.

4
  • Jon, my code above didn't work. Actually the web service code converts a string (containing XSLFO data) into byte array using utf8 encoding. In my web service response I only see data something like "PGZvOnJvb3QgeG1sbnM6Zm89Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML0Zvcm1hdCIgeG1sbnM6eGY9Imh0dHA6Ly93d3cuZWNyaW9uLmNvbS94Zi8xLjAiIHhtbG5zOm1zeHNsPSJ1c==". Actually I would like to have the original string value that was converted into byte[] in the service. Not sure if it possible? Commented May 6, 2011 at 19:04
  • @user465876: That looks like Base64 to me... so Convert.FromBase64String should be fine. But if it was originally a string, why was it converted into a byte array to start with? Commented May 6, 2011 at 19:47
  • It worked with Convert.FromBase64String. Actually same webservice is being called from different places and in most cases clients work with byte array. Commented May 6, 2011 at 19:54
  • See also this Phil Haack blog post Commented Jan 30, 2012 at 9:31
0

If the string is encoded in UTF8 Encoding, then yes that is the correct way. If it is in Unicode it is very similar:

System.Text.Unicode encoding = new System.Text.Unicode();
byte[] bytes = encoding.GetBytes(inputString);

Base64Encoding is a little different:

byte[] bytes = Convert.FromBase64String(inputString);
0

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.