Native CRC

I don’t suppose we could get, or is there already a native CRC function? CRC16 would be dandy.

Thanks

http://docs.ghielectronics.com/software/tinyclr/tutorials/hashing.html

Are you planning to add crc32 implementation?

We actually already have it internally. Not sure why we didn’t expose it.

Please expose it.

2 Likes

Are you expose native crc32 in release 2.0 ?

Can you use MD5 or your project require has to be CRC?

I was planning to use CRC

Sorry, it is too late in v2.0.0.

In 2.0.1 maybe?

99% is Yes.

Why not use md5?

in managed code


//https://stackoverflow.com/questions/8128/how-do-i-calculate-crc32-of-a-string?noredirect=1&lq=1

using System;

namespace TinyCLRCRC32
{
     class CRC32
    {
        private uint[] ChecksumTable;
        private uint Polynomial = 0xEDB88320;

        public CRC32()
        {
            ChecksumTable = new uint[0x100];

            for (uint index = 0; index < 0x100; ++index)
            {
                uint item = index;
                for (int bit = 0; bit < 8; ++bit)
                    item = ((item & 1) != 0) ? (Polynomial ^ (item >> 1)) : (item >> 1);
                ChecksumTable[index] = item;
            }
        }

        public byte[] ComputeHash(byte[] stream)
        {
            UInt32 result = 0xFFFFFFFF;
            int current;

            for (int i = 0; i < stream.Length; i++)
            {
                current = stream[i];
                result = ChecksumTable[(result & 0xFF) ^ (byte)current] ^ (result >> 8);
            }

            byte[] hash = BitConverter.GetBytes(~result);
            byte[] reverseHash = Reverse(hash);

            return Reverse(reverseHash);
        }

        private byte[] Reverse(byte[] Array)
        {
            byte[] toReverse = new byte[Array.Length];

            int j = 0;

            for (int i = Array.Length - 1; i >= 0; i--)
            {
                toReverse[j] = Array[i];
                j++;
            }
            return toReverse;
        }
    }
}

usage


using System;
using System.Diagnostics;
using System.Text;

namespace TinyCLRCRC32
{
    class Program
    {
        static void Main()
        {
            Debug.WriteLine("CRC32 Test 1!");

            var crc = new CRC32();

            // Convert a C# string to a byte array  
            string author = "Valon Hoti";


            // Convert a C# string to a byte array  
            ////convert binary file to byte[] ...
            byte[] bytes = Encoding.UTF8.GetBytes(author);
            string myRes = "";
            foreach (byte s in crc.ComputeHash(bytes))
            {
                myRes += s.ToString("x2").ToUpper();
            }

            Debug.WriteLine(myRes); //need to reverse it because MCU and DESKTOP act differently
            Debug.WriteLine(stringReverseString(myRes));

            while(true)
            {

            }
        }

    public static string stringReverseString(string str)
        {
            char[] chars = str.ToCharArray();
            for (int i = 0, j = str.Length - 1; i < j; i++, j--)
            {
                char c = chars[i];
                chars[i] = chars[j];
                chars[j] = c;
            }
            return new string(chars);
        }
    }
}

REMARKS.
**TESTED ON DESKTOP AND TINYCLR but results are different **

CODE

@Dat_Tran , @Gus_Issa

      var crc = new CRC32();

            // Convert a C# string to a byte array  
            string author = "Valon Hoti";

         ////convert binary file to byte[] ...
            byte[] bytes = Encoding.UTF8.GetBytes(author);
            string myRes = "";
            foreach (byte s in crc.ComputeHash(bytes))
            {
                myRes += s.ToString("x2").ToUpper();
            }

            Debug.WriteLine(myRes);

gave

result : 770B5A89 on DESKTOP
result: 895A0B77 on TINYCLR

after i add stringReverseString to correct it

but slower than native

Correct but SITCore is 30 times faster than G120

1 Like