您放置的示例代碼應將字符串轉換爲字節數組。根據您使用的編碼(例如,ASCII,Unicode等),您可能會從同一個字符串中獲取不同的字節數組。
術語數據包通常用於通過網絡發送數據;但數據包本身只是字節數組。
你得到的信息讀取myUsername,myPassword。下面的C#代碼將爲您翻譯。
byte[] packet = new byte[] { 0x22, 0x00, 0x11, 0x00, 0x6D, 0x79, 0x75, 0x73, 0x65, 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x79, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
string test = Encoding.ASCII.GetString(packet);
Console.WriteLine(test);
Console.ReadKey();
所以創建類似的東西我想嘗試:
const int HeaderLength = 2;
const int UsernameMaxLength = 16;
const int PasswordMaxLength = 16;
public static byte[] CreatePacket(int header, string username, string password)//I assume the header's some kind of record ID?
{
int messageLength = UsernameMaxLength + PasswordMaxLength + HeaderLength;
StringBuilder sb = new StringBuilder(messageLength+ 2);
sb.Append((char)messageLength);
sb.Append(char.MinValue);
sb.Append((char)header);
sb.Append(char.MinValue);
sb.Append(username.PadRight(UsernameMaxLength, char.MinValue));
sb.Append(password.PadRight(PasswordMaxLength, char.MinValue));
return Encoding.ASCII.GetBytes(sb.ToString());
}
然後調用此代碼:
byte[] myTest = CreatePacket(17, "myusername", "mypassword");
考慮到你對先前類似問題中的預覽鏈接評論的反應如何,我不願意發表這個,但可能[this](http://stackoverflow.com/questions/10043621/create-a-network-在數據包中快速發送並通過網絡發送)可以提供幫助? – psubsee2003
遊戲或網絡包需要更多解釋 – Yohannes
這是一個從遊戲中嗅探出來的數據包。 –