-1

I need to use this class: Source: 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;
            }
        }
    }
}

I have an array of bytes and I need to display the CRC32 checksum of that array in a text box as a hexadecimal representation when I press a button. For example:

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

Can you please help with that as I am still new to programming.

shox
  • 677
  • 1
  • 5
  • 19
Daniel Y
  • 13
  • 2
  • What's the problem? You need to know how to format the result? – shox Nov 20 '20 at 18:40
  • Formatting result is not a problem, my problem is I don't know how to pass my byte array to the class, how to construct it.... – Daniel Y Nov 20 '20 at 20:16

1 Answers1

0

Assuming I understand your question correctly, it's that you don't understand how to call a method within a class.

First you will need to instantiate your class into and object, then you can call the methods within the class.

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

I'd suggest looking at some beginner resources to better understand object oriented programming in C#.

shox
  • 677
  • 1
  • 5
  • 19
  • 1
    You've got my question correctly and I am very happy that code worked out perfectly exactly as I needed. Thanks for suggestion, your help and time. – Daniel Y Nov 21 '20 at 14:02
  • Glad I could help. Best of luck on your journey (of learning to program). Stick with it and I'm sure you'll do great. – shox Nov 21 '20 at 21:41