繁体   English   中英

如何在我自己的代码中使用这个 Crc32 类

[英]How to use this Crc32 class in my own code

我需要使用这个类:来源: http : //www.sanity-free.com/12/crc32_implementation_in_csharp.html

public class Crc32 {
        uint[] table;

        public uint ComputeChecksum(byte[] bytes) {
            uint crc = 0xffffffff;
            for(int i = 0; i < bytes.Length; ++i) {
                byte index = (byte)(((crc) & 0xff) ^ bytes[i]);
                crc = (uint)((crc >> 8) ^ table[index]);
            }
            return ~crc;
        }

        public byte[] ComputeChecksumBytes(byte[] bytes) {
            return BitConverter.GetBytes(ComputeChecksum(bytes));
        }

        public Crc32() {
            uint poly = 0xedb88320;
            table = new uint[256];
            uint temp = 0;
            for(uint i = 0; i < table.Length; ++i) {
                temp = i;
                for(int j = 8; j > 0; --j) {
                    if((temp & 1) == 1) {
                        temp = (uint)((temp >> 1) ^ poly);
                    }else {
                        temp >>= 1;
                    }
                }
                table[i] = temp;
            }
        }
    }
}

我有一个字节数组,当我按下按钮时,我需要在文本框中显示该数组的 CRC32 校验和作为十六进制表示。 例如:

byte [] my_bytes = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
textBox1.Text = // the checksum of my_bytes as hex

你能帮忙解决这个问题吗,因为我还是编程新手。

假设我正确理解了您的问题,那就是您不了解如何在类中调用方法。

首先,您需要将类实例化为对象,然后您可以调用类中的方法。

byte [] myBytes = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
var crc32Instance = new Crc32();
var resultingBytes = crc32Instance.ComputeChecksumBytes(myBytes);
var byteString = String.Concat(Array.ConvertAll(resultingBytes , x => x.ToString("X2")));
textBox1.Text = byteString// the checksum of my_bytes as hex

我建议查看一些初学者资源以更好地理解 C# 中的面向对象编程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM