Krimson
Krimson

Reputation: 7664

VB.NET convert UCS-2 bytes to ASCII

Ok so i am working on this program which sends a packet to a minecraft server and in return it gives me information about the server;(message of the day, players online, max players)

The problem is the response is in UCS-2

So when i send the packet to the server and get the response in bytes. How do i convert it to ascii so i can work with it?

Here is my code so far

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
....

Here is the same code in PHP, python, and ruby.


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


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


ruby -> http://pastebin.com/q5zFPcXV

The documentation is here: http://www.wiki.vg/Protocol#Server_List_Ping_.280xFE.29

Thanks in advance!
Vidhu

Upvotes: 0

Views: 1444

Answers (1)

SSS
SSS

Reputation: 5403

Tested and working.

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

Upvotes: 2

Related Questions