我玩弄RCON協議,但我有問題將字符串成功轉換爲字節數組。C# - 字節轉換不是因爲它應該
原始代碼(VB.NET)+引擎收錄:http://pastebin.com/4BkbTRfD
Private Function RCON_Command(ByVal Command As String,
ByVal ServerData As Integer) As Byte()
Dim Packet As Byte() = New Byte(CByte((13 + Command.Length))) {}
Packet(0) = Command.Length + 9 'Packet Size (Integer)
Packet(4) = 0 'Request Id (Integer)
Packet(8) = ServerData 'SERVERDATA_EXECCOMMAND/SERVERDATA_AUTH (Integer)
For X As Integer = 0 To Command.Length - 1
Packet(12 + X) = System.Text.Encoding.Default.GetBytes(Command(X))(0)
Next
Return Packet
End Function
我當前的代碼在C#+引擎收錄:http://pastebin.com/eVv0nZCf
byte[] RCONCommand(string cmd, int serverData)
{
int packetSize = cmd.Length + 12;
byte[] byteList = new byte[packetSize];
byteList[0] = (byte)packetSize;
byteList[4] = 0;
byteList[8] = (byte)serverData;
for(int X = 0; X < cmd.Length; X++)
{
byteList[12 + X] = Encoding.ASCII.GetBytes(cmd)[X];
}
return byteList;
}
當我使用Encoding.ASCII.GetString(RCONCommand(」單詞「,3));結果將是方形標記。我也試過Encoding.UTF8.GetString(),但結果相同。
包結構可以在這裏找到:https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Basic_Packet_Structure
我不只是弄清楚什麼我做錯了,因爲我連一個熟悉字節和這樣的。 PS。源於RCON協議文檔的C#中發佈的示例應用程序出現亂碼,因爲人們使用了太多的OOP並創建了數百萬個類文件,所以我甚至找不到正確的東西。
'IPAddress.HostToNetworkOrder(size)'可以少點出價更簡單 – Fabio