/// <summary> /// Converts bytes received as data from the server in to managable data /// </summary> /// <param name="bytes">The bytes to be converted</param> /// <returns>Managable BuddyListData array</returns> public static BuddyListData[] BytesToBuddyListData(byte[] bytes) { ArrayList returnVal = new ArrayList(); int baseCount = 0; while (baseCount < bytes.Length) { BuddyListData data = new BuddyListData(); data.userID = System.BitConverter.ToInt32(bytes,baseCount); int usernameLength = System.BitConverter.ToInt32(bytes, baseCount + 4); data.username = System.Text.UnicodeEncoding.Unicode.GetString(bytes, baseCount + 8, usernameLength); data.userState = (UserState)System.BitConverter.ToInt32(bytes, baseCount + 8 + usernameLength); baseCount += 12 + usernameLength; returnVal.Add(data); } return (BuddyListData[])returnVal.ToArray(typeof(BuddyListData)); }
/// <summary> /// Converts BuddyListData[] into bytes to be sent across the network /// </summary> /// <param name="data">The BuddyListData to convert</param> /// <returns>Bytes representing the given BuddyListData</returns> public static byte[] BuddyListDataToBytes(BuddyListData[] data) { ArrayList temp = new ArrayList(); for (int i = 0; i < data.Length; i++) { byte[] usernameBytes = System.Text.UnicodeEncoding.Unicode.GetBytes(data[i].username); temp.AddRange(System.BitConverter.GetBytes(data[i].userID)); temp.AddRange(System.BitConverter.GetBytes(usernameBytes.Length)); temp.AddRange(usernameBytes); temp.AddRange(System.BitConverter.GetBytes((int)data[i].userState)); } return (byte[])temp.ToArray(typeof(byte)); }