2012-10-04 131 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