从字节数组解析字符串,将字节数组转换为字符串 [英] Parsing string from byte array, Convert byte array to string

查看:106
本文介绍了从字节数组解析字符串,将字节数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析;rb(7167) As Byte或将其转换为String.

我的代码示例如下:

I want parse or convert ;rb(7167) As Byte to String.

My code example is below:

Sub help()
    Dim HttpWebRequest As HttpWebRequest, HttpWebResponse As HttpWebResponse
    HttpWebRequest = WebRequest.Create("http://www.google.com")
    HttpWebResponse = HttpWebRequest.GetResponse
    Dim rb(64) As Byte
    Dim tbr As Integer
    tbr = HttpWebResponse.GetResponseStream.Read(rb, 0, 65)
    Dim Str As String = ""
    'I did
    For i = 0 To tbr-1
        Str = Str & Strings.Chr(rb(i))
    Next
    'But i need some thing simple and full data.
End Sub

推荐答案

看看msdn上的示例.这样可以帮助您:
http://msdn.microsoft.com/en-us/library/system. net.httpwebresponse.getresponsestream.aspx [ ^ ]

祝您好运!
Have a look at the example on msdn. That will help you out:
http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx[^]

Good luck!


一个建议:您的代码在字符串连接&"中显示出非常差的性能.为此,您应始终使用Text.StringBuilder.Append.

这就是为什么.
字符串是不可变的.这意味着字符串连接永远不会修改字符串.像在所有字符串操作中一样,将创建一个全新的字符串并复制其他字符串的内容.如果您以循环方式执行此操作,则将不断创建不断增长的目标字符串实例,并一遍又一遍地复制前一个结果. Text.StringBuilder.Append确实修改了StringBuilder的实例,因此可以有效地解决问题.

顺便说一句,string.Format对于一次性操作也更有效,但不应循环使用,因为只有Text.StringBuilder才能很好地发挥作用.

正如我已经说过的,请不要使用",请使用string.Empty.

—SA
A side advice: your code shows very poor performance in string concatenation "&". Your should always use Text.StringBuilder.Append for such purposes.

Here is why.
Strings are immutable. It means that string concatenation never modifies a string. Like in all string operations, a brand new string is created and the content of tho other strings are copied. If you do this in cycle, your ever-increasing target string instance is re-created with full copy of the previous result over and over. Text.StringBuilder.Append really modifies the instance of the StringBuilder, so it solves the problem efficiently.

By the way, string.Format is also more efficient for one-time operation, but should not be used in cycle, where only Text.StringBuilder does a good job.

As I already said, don''t use "", use string.Empty.

—SA


这篇关于从字节数组解析字符串,将字节数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆