开发者

How can i write a response.binarywrite to a string

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 开发者_JAVA技巧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


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.


Use an encoding to convert it back.

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜