Sys(2007) in c#

I have a foxpro code which I need to convert into c#.

I am stuck at this line of code

SYS(2007,ALLTRIM(clipmade.rcompany))

could any one help me in coverting this to c# code
ziorinfoAsked:
Who is Participating?
 
MajorBigDealCommented:
Code 2007 is CRC32 checksum.

I googled it and was disappointed to find that C# doesn't have a built in method for this.  However if you google "c# crc32", you will get a ton of hits on various ways of implementing in c#.   Here is an example that is highly-rated (over 4 stars) but there are lots of others as well.

http://www.codeproject.com/KB/cs/marcelcrcencoding.aspx

ALLTRIM is the same as C# Trim();

So, using the example implementation in the above link, and assuming clipmade.rcompany is a string: compCRC.crctablefast(clipmade.rcompany.Trim())
0
 
nishant joshiTechnology Development ConsultantCommented:
add this class to your project..
using System;

public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }

public class Crc16Ccitt {
    const ushort poly = 4129;
    ushort[] table = new ushort[256];
    ushort initialValue = 0;

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

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

    public Crc16Ccitt(InitialCrcValue initialValue) {
        this.initialValue = (ushort)initialValue;
        ushort temp, a;
        for(int i = 0; i < table.Length; ++i) {
            temp = 0;
            a = (ushort)(i << 8);
            for(int j = 0; j < 8; ++j) {
                if(((temp ^ a) & 0x8000) != 0) {
                    temp = (ushort)((temp << 1) ^ poly);
                } else {
                    temp <<= 1;
                }
                a <<= 1;
            }
            table[i] = temp;
        }
    }
}

Open in new window


and then your c# code will be..
 
Crc16Ccitt crc=new Crc16Ccitt(InitialCrcValue.NonZero2);
crc.ComputeChecksum(((Bytes)(clipmade.rcompany).Trim()));

Open in new window


regards
0
 
Olaf DoschkeSoftware DeveloperCommented:
Crc16citt instead of CRC32? Sure? Actually you are right, because only if a fourth, optional parameter would be 1, sys(2007) would compute CRC32, as it's not, SYS(2700,string) computes CRC16 of the string.

Also note, Sys(2700) returns a string! Even though CRC16 computes a number, this is returned as a string. So for compatibility you'd need to convert the result .ToString().

To give you some examples for testing the implementation:
sys(2700,"abc") -> "20810"
sys(2700,"xyz") -> "53289"
sys(2700,"ziorinfo") -> "23948"

See if you get those exact numbers from Crc16Ccitt.
Depending on the usage of the checksum you may replac eit with a total different checksum mechanism, and eg recompute all currently stored checksums.

As a much more general help, the reference on VFP commands and functions is online at MSDN:
http://msdn.microsoft.com/en-us/library/74zkxe2k%28VS.80%29.aspx

Sys() function for example here:
http://msdn.microsoft.com/en-us/library/65zx7k19%28VS.80%29.aspx

Actually the Sys() functions are functions, that didn't make it into getting a real function name. Some nevertheless have, eg SYS(0) is also ID(). But that's just a side note.

You also can get the full help as a chm, as it's been given to the fox community under a codeplex shared source license: http://vfpx.codeplex.com/releases/view/23319

Besides this, there is a toolkit implementing some foxpro functions, that also could help you keep it compatible on those: http://foxcentral.net/microsoft/vfptoolkitnet.htm

Bye, Olaf.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.