如何从此代码中获取字符串而不是char数组?
客户代码:
stm = tcpClient.GetStream()
Dim ascenc As New ASCIIEncoding
Dim byteData() As Byte = ascenc.GetBytes(strMessage(counter))
Thread.Sleep(2000)
Console.WriteLine("Transmitted ")
stm.Write(byteData, 0, byteData.Length())
服务器代码:
Dim size As Integer = TcpSocket.Receive(bitData)
Dim chars(size) As Char
For i As Integer = 0 To size
chars(i) = Convert.ToChar(bitData(i)) // i want to get the string directly, how ?
Next
Dim newString As New String(chars)
Console.WriteLine(newString)
strMessage(counter) = newString
答案 0 :(得分:1)
您已经使用我建议的代码实现了它:
Dim MyString As String = New String(MyArray)
如果要转换字节数组,可以使用:
Dim MyString As String = Encoding.ASCII.GetString(bytes)
答案 1 :(得分:1)
您也可以在服务器上使用ASCIIEncoding
。只需使用GetString()
method而不是GetBytes()
。
Dim ascenc As New ASCIIEncoding
Dim newString As String = ascenc.GetString(bitData)