使用C#压缩数据 [英] Compressing data using c#

查看:72
本文介绍了使用C#压缩数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要编写游程编码逻辑"用于字节输入.

///例如,aaaaaaabbbbccccddddd将被编码为

        ///
        ///7a4b4c5d
        ///
以下逻辑对实现这一目标有意义吗?

--------------------------------------------------- -------------------------------------------------- ---------------

对于字节输入,例如:

新字节[] {0x01、0x01、0x01、0x02、0x01、0x03、0x01、0x04}

公共字节[]编码(原始字节[])
{
        byte runLength;
        List< byte> dest = new List< byte>();


       为(int i = 0; i< original.Length; i ++)
        {
            runLength = 1;
            while(runLength< byte.MaxValue 
                &&我+1原始长度.
                && original [i] == original [i + 1])
            {
                runLength ++;
                i ++;
            }
            dest.Add(runLength);
            dest.Add(original [i]);
        }

        return dest.ToArray();
}

解决方案

上面有任何建议吗?谢谢.


Hello,

I need to write "run length encoding logic" for byte inputs.

/// For example, aaaaaaabbbbccccddddd would be encoded as

        ///
        /// 7a4b4c5d
        ///
Does the following logic make sense to achieve this?

----------------------------------------------------------------------------------------------------------------

For e.g.byte input could be like:

new byte[]{0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04}

public byte[] Encode(byte[] original)
{
       byte runLength;
       List<byte> dest = new List<byte>();


        for (int i = 0; i < original.Length; i++)
        {
            runLength = 1;
            while (runLength < byte.MaxValue 
                && i + 1 < original.Length 
                && original[i] == original[i + 1])
            {
                runLength++;
                i++;
            }
            dest.Add(runLength);
            dest.Add(original[i]);
        }

        return dest.ToArray();

解决方案

Any suggestion on above please?Thanks.


这篇关于使用C#压缩数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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