RPS
RPS

Reputation: 837

Convert SQL Binary Data to String in C#

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

Upvotes: 4

Views: 5817

Answers (3)

cognophile
cognophile

Reputation: 927

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()

Upvotes: 0

Koen
Koen

Reputation: 3683

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);

Upvotes: 3

Anton Gogolev
Anton Gogolev

Reputation: 115867

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);

Upvotes: 1

Related Questions