我如何将response.binarywrite写入字符串
我将数据作为 Longblob 保存在数据库中,在保存时我将其转换为字节并保存,同时检索我们可以通过如下方式编写该数据
Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
Response.BinaryWrite(bytes);
但我需要的是我需要将字节数据表示为字符串是否可以
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么,数据是编码字符串吗?如果是这样,请使用类似
...的内容,但请确保使用正确的编码。
如果它是任意二进制数据,您应该使用 base64 代替:
这将为您提供一个完全 ASCII 字符串,可以使用
Convert.FromBase64String
将其转换回原始字节数组。如果不知道您的数据是什么,就不可能判断哪种方法合适。您说保存时将数据转换为字节 - 但您没有说如何将数据转换为字节。基本上你想要扭转这个过程,无论它是什么。
Well, is the data an encoded string? If so, use something like
... but make sure you use the right encoding.
If it's arbitrary binary data though, you should use base64 instead:
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.
我认为这就是您所需要的: http://msdn.microsoft.com/en -us/library/744y86tc.aspx
I think this is what you need: http://msdn.microsoft.com/en-us/library/744y86tc.aspx
您可以使用 System.Text.Encoding.GetString(byte[]) 方法将字节数组转换为字符串,但您确实需要指定字节采用的编码。CLR 附带了一些默认编码 (Encoding.UTF8) 、Encoding.ASCII 等),但一般来说,您需要非常了解使用什么编码来对数据进行编码。
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.