C#string与byte数组转行
/// <summary> /// string[]-->buffer[] /// </summary> /// <param name="msg"></param> internal static Byte[] StringArrayToBufferArray(string[] array) { Byte[] buffer = new Byte[1024]; using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter wr = new BinaryWriter(stream)) { for (Int32 i = 0; i < array.Length; i++) { wr.Write(array[i]); } } //转换成字节数组,用于Socket发送 buffer = stream.ToArray(); return buffer; } } /// <summary> /// byte[]-->string[] /// </summary> /// <param name="buffer"></param> /// <param name="count">数组长度</param> /// <returns></returns> internal static String[] BufferArrayTOStringArray(Byte[] buffer, Int32 count) { List<String> list = new List<String>(); using (MemoryStream stream = new MemoryStream(buffer)) { using (BinaryReader rd = new BinaryReader(stream)) { try { while (stream.Position < stream.Length) { if (list.Count < count) { list.Add(rd.ReadString()); } else { break; } } rd.Close(); } catch { } } String[] array = list.ToArray(); return array; } } /// <summary> /// string->string[] /// </summary> /// <param name="strarry"></param> /// <returns></returns> internal static String StringArryToString(String[] strarry) { String str = String.Empty; for (Int32 i = 0; i < strarry.Length; i++) { str += strarry[i]; } return str; }