The sample code you've put should convert the string to a byte array. Depending which encoding you use (e.g. ASCII, Unicode, etc) you may get a different byte array from the same string.
The term packet is generally used when you're sending data through a network; but the packet itself is just the byte array.
The info you've got reads myUsername, myPassword. The below C# code will translate for you.
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();
So to create something similar I'd try:
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());
}
Then call this code with:
byte[] myTest = CreatePacket(17, "myusername", "mypassword");