0

I am having my data saved in my database as Longblob and while saving i am converting it to bytes and saving now while retrieving we can have that data by writing as follows

 Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
                    Response.BinaryWrite(bytes);

But what i need is i need that data in bytes to be represented as string is it possible

1
  • Is the binary data just string character codes (like ASCII or UTF-8/16), or is it encoded in some fashion (e.g. BASE 64) Commented Apr 6, 2011 at 15:56

4 Answers 4

5

Well, is the data an encoded string? If so, use something like

string text = Encoding.UTF8.GetString(bytes);

... but make sure you use the right encoding.

If it's arbitrary binary data though, you should use base64 instead:

string text = Convert.ToBase64String(bytes);

That will get you an entirely-ASCII string which can be converted back to the original byte array using Convert.FromBase64String.

Without knowing what your data is, it's impossible to say which of these approaches is appropriate. You say you're converting your data to bytes when you save - but you haven't said how you've converted your data to bytes. Basically you want to reverse that process, whatever it is.

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

Comments

0

Use an encoding to convert it back.

string text = System.Text.Encoding.ASCII.GetString(myByteArray);

Comments

0

I think this is what you need: http://msdn.microsoft.com/en-us/library/744y86tc.aspx

Comments

0

You can convert arrays of bytes to strings by using the System.Text.Encoding.GetString(byte[]) method, but you do need to specify which encoding the bytes are in. The CLR comes with a few default encodings (Encoding.UTF8, Encoding.ASCII, etc) but in general you need to be very aware of what encoding was used to encode the data.

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.