4

Does anybody have sample code to convert from binary data to string in c#?

3
  • Take note here, if your data is binary because it is encrypted, you must decrypt it before you can read it's data. Commented Jun 9, 2010 at 15:32
  • With binary data, you mean a byte array (byte[])? Commented Jun 9, 2010 at 15:32
  • What are you trying to accommplish? We could go about this any many ways and most will be the wrong way if we don't have more information about what you are trying to accomplish and what is in the binary data. We could decode it by parsing the data, or we could encode it in base64, or decode it to unicode. What does the binary data represent? Commented Jun 9, 2010 at 16:50

3 Answers 3

3

Sounds like you just need to decode the binary data. Therefore you need an encoding (like utf-8 or unicode).

Example:

var textFromBinary = System.Text.Encoding.UTF8.GetString(myBinaryData);
Sign up to request clarification or add additional context in comments.

Comments

1

If your binary data is stored in byte array and you want to convert it to Base64-encoding, you can use Convert.ToBase64String method:

var base64String = Convert.ToBase64String(yourBinaryDataHere);

Comments

0

In modern versions of .NET, we can simply use the .ToString() override of BinaryData for a UTF-8 decoding of the contained bytes.

string data = myBinaryData.ToString()

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.