Link to home
Start Free TrialLog in
Avatar of Mahesh Yadav
Mahesh YadavFlag for India

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of MajorBigDeal
MajorBigDeal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nishant joshi
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
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.