To find out the "size" (in kb) of a string, we need to know the encoding. If we assume UTF8, then this (not including the specification, etc.) As shown below (but replace the encoding if it is not UTF8):
int len = Encoding.UTF8.GetByteCount(longString);
Put it down; I would suggest GZIP via UTF8, optionally followed by base-64 if it should be a string:
using (MemoryStream ms = new MemoryStream()) { using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true)) { byte[] raw = Encoding.UTF8.GetBytes(longString); gzip.Write(raw, 0, raw.Length); gzip.Close(); } byte[] zipped = ms.ToArray();
Marc gravell
source share