public static byte[] ToBytes(IEnumerable <string> scanIds) { var listStartBytes = new List <int>(); var listLengths = new List <int>(); var listIdBytes = new List <byte>(); foreach (var scanId in scanIds) { int len = Encoding.UTF8.GetByteCount(scanId); var buffer = new byte[len]; Encoding.UTF8.GetBytes(scanId, 0, scanId.Length, buffer, 0); listStartBytes.Add(listIdBytes.Count); listLengths.Add(len); listIdBytes.AddRange(buffer); } if (listStartBytes.Count == 0) { return(new byte[0]); } var listEntryBytes = new List <byte>(); listEntryBytes.AddRange(PrimitiveArrays.ToBytes(listStartBytes.ToArray())); listEntryBytes.AddRange(PrimitiveArrays.ToBytes(listLengths.ToArray())); var entryBytes = listEntryBytes.ToArray(); var entryBytesCompressed = entryBytes.Compress(); var scanIdBytes = listIdBytes.ToArray(); var scanIdBytesCompressed = scanIdBytes.Compress(); var listResultBytes = new List <byte>(); listResultBytes.AddRange(BitConverter.GetBytes(entryBytes.Length)); listResultBytes.AddRange(BitConverter.GetBytes(entryBytesCompressed.Length)); listResultBytes.AddRange(BitConverter.GetBytes(scanIdBytes.Length)); listResultBytes.AddRange(BitConverter.GetBytes(scanIdBytesCompressed.Length)); listResultBytes.AddRange(entryBytesCompressed); listResultBytes.AddRange(scanIdBytesCompressed); return(listResultBytes.ToArray()); }