字符串压缩增加了字符串的长度 [英] String Compression increasing length of string

查看:125
本文介绍了字符串压缩增加了字符串的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有一个生成字符串的函数
那串很长.
我想减小其长度和尺寸

这是我的代码和结果:
没有发布真正的字符串,这很长

hi all
i have one function which generates string
that string is very lengthy.
i want to decrease its length and size

this is my code and result:
not posting real string it is very long

protected void Page_Load(object sender, EventArgs e)
    {
        string txt = "somestring";
        Label1.Text = txt.Length.ToString();
        string cmpString = Compress(txt);
        Label2.Text = cmpString.Length.ToString();
    }
 
//For Compression
    public string Compress(string text)
            {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
            {
            zip.Write(buffer, 0, buffer.Length);
            }
            ms.Position = 0;
            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);
            byte[] gzBuffer = new byte[compressed.Length + 4];
            System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
            return Convert.ToBase64String(gzBuffer);
    
    }



结果:
465832
667296



我的问题是,当我压缩数据时,该字符串的长度应减小,这​​是增加的
有什么解决方案可以解决,我想最小化其长度和大小.



result:
465832
667296



my question is when i m compressing the data the length of that string should reduce it is increasing
is there any solution becoz i want to minimize its length and size.

推荐答案


您是将字符串转换为字节,然后再次以字符串格式显示吗?
无论如何,字节的字符串表示形式将具有很大的长度和大小.仅将其存储为字节.


--Amit
Hi,
You are converting your string to byte and again displaying it in string format?
The string representation of byte will be big in length and size anyway. Store it in byte only.


--Amit


这篇关于字符串压缩增加了字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆