VB.NET将UCS-2字节转换为ASCII

时间:2012-10-04 01:55:24

标签: vb.net tcp character-encoding byte ascii

好的,我正在研究这个程序,它将一个数据包发送到一个Minecraft服务器,作为回报,它给了我关于服务器的信息;(当天的消息,在线玩家,最大玩家)

问题是响应是在UCS-2

因此,当我将数据包发送到服务器并以字节为单位获取响应时。我如何将其转换为ascii,以便我可以使用它?

到目前为止,这是我的代码

Dim client As New System.Net.Sockets.TcpClient()
client .Connect("178.33.213.54", 25565)

Dim stream As NetworkStream = client .GetStream



'Send Bytes
Dim sendBytes As [Byte]() = {&HFE}
stream.Write(sendBytes, 0, sendBytes.Length)

'Receive Bytes
Dim bytes(client .ReceiveBufferSize) As Byte
stream.Read(bytes, 0, CInt(leclient.ReceiveBufferSize))

'Convert it to ASCII
....


'Output it to Console
....

这是PHP,python和ruby中的相同代码。


php - > https://gist.github.com/1235274


python - > https://gist.github.com/1209061


红宝石 - > http://pastebin.com/q5zFPcXV

文档在这里: http://www.wiki.vg/Protocol#Server_List_Ping_.280xFE.29

提前致谢!
Vidhu

1 个答案:

答案 0 :(得分:2)

经过测试和工作。

Dim client As New System.Net.Sockets.TcpClient()

client.Connect("178.33.213.54", 25565)

Dim stream As System.Net.Sockets.NetworkStream = client.GetStream

'Send Bytes
Dim sendBytes As [Byte]() = {&HFE}
stream.Write(sendBytes, 0, sendBytes.Length)

''Receive Bytes
Dim bytes(client.ReceiveBufferSize) As Byte
stream.Read(bytes, 0, CInt(client.ReceiveBufferSize))

Dim sb As New System.Text.StringBuilder
For i As Integer = 3 To bytes.GetUpperBound(0) Step 2
  Dim byt2(1) As Byte
  byt2(0) = bytes(i + 1)
  byt2(1) = bytes(i)

  Dim ii As Integer = BitConverter.ToInt16(byt2, 0)
  'sb.Append(Hex(ii)) 'debug
  sb.Append(ChrW(ii))
Next i
MsgBox(sb.ToString)
stream.Close()