/// <summary> /// 重载的方法,将 cHandshake 命令转化为二进制数据 /// </summary> /// <returns></returns> public override byte[] ToBinary( ) { List <byte> ret = new List <byte>( ); ret.AddRange(BitConverter.GetBytes(( Int32 )this.SyncMode)); for (int i = 0; i < this.PersonnelList.Count; i++) { PerInfo pi = PersonnelList[i]; ret.AddRange(tcpCommandCore.CStr2Bin(pi.PID, 10)); ret.AddRange(tcpCommandCore.CStr2Bin(pi.FID, 10)); ret.AddRange(tcpCommandCore.CStr2Bin(pi.Name, 40)); ret.AddRange(tcpCommandCore.CStr2Bin(pi.Department, 100)); if (pi.Eigenvalue == null || pi.Eigenvalue.Length <= 0) { ret.Add(0x00); } else { ret.Add(0x01); byte[] tmp = new byte[193]; Array.Copy(pi.Eigenvalue, tmp, 193); ret.AddRange(tmp); } } return(GenerateBinary(ret.ToArray( ))); }
/// <summary> /// 从 byte 数组中提取数据生成 cHandshake 对象 /// </summary> /// <param name="data"></param> /// <returns></returns> public new static tcpSyncPersonnel Parse(byte[] data) { tcpSyncPersonnel ret = new tcpSyncPersonnel( ) { _id = BitConverter.ToInt16(data, 0), _number = BitConverter.ToInt32(data, 2), SyncMode = ( SyncRange )BitConverter.ToInt32(data, 6) }; if (data.Length > 10) { int sIdx = 10; while (true) { PerInfo item = new PerInfo( ); item.PID = tcpCommandCore.CBin2Str(data, sIdx, 10); // 0 .. 9 item.FID = tcpCommandCore.CBin2Str(data, sIdx + 10, 10); // 10 .. 19 item.Name = tcpCommandCore.CBin2Str(data, sIdx + 20, 40); // 20 .. 59 item.Department = tcpCommandCore.CBin2Str(data, sIdx + 60, 100); // 60 .. 159 if (data[sIdx + 160] == 0x00) { item.Eigenvalue = null; sIdx = sIdx + 161; } else { item.Eigenvalue = new byte[193]; Array.Copy(data, sIdx + 161, item.Eigenvalue, 0, 193); sIdx = sIdx + 354; } ret.PersonnelList.Add(item); if (sIdx >= data.Length) { break; } } } return(ret); }