2012-10-04 80 views
0

好吧,我正在开发这个程序,它向Minecraft服务器发送一个数据包,并作为回报,它提供了有关服务器的信息;(当天消息,玩家在线,max玩家)VB.NET将UCS-2字节转换为ASCII

的问题是响应为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


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


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

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

在此先感谢!
Vidhu

回答

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() 
+1

P.S.这转换为Unicode,而不是ASCII,因为Unicode是.NET字符串的本地格式 – SSS

+0

嗨,感谢您的代码! 你能告诉我你为什么踩2吗? – Krimson

+0

在你的问题中你说输入流是在UCS-2中。根据维基百科的说法:“UTF-16和UCS-2都将这一范围内的代码点编码为单个16位代码单元,这些代码单元在数值上等于相应的代码点。” 我正在步进2,因为16位= 2字节 – SSS