Checksum crc-16

sandmen

Mitglied
Hallo,
in meinem Vorhanden C# Programm ist eine Checksum Berechnung enthalten.
Ich möchte/muss diese in Java umsetzen.
Das ist der C# code.

Code:
      private ushort crc_accumulate(byte b, ushort crc)
        {
            unchecked
            {
                byte ch = (byte)(b ^ (byte)(crc & 0x00ff));
                ch = (byte)(ch ^ (ch << 4));
                return (ushort)((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4));
            }
        }

        private ushort crc_calculate(byte[] pBuffer, int length)
        {
            if (length < 1)
            {
                return 0xffff;
            }

            ushort crcTmp;
            int i;

            crcTmp = X25_INIT_CRC;

            for (i = 1; i < length; i++) // skips header U
            {
                crcTmp = crc_accumulate(pBuffer[i], crcTmp);
                //Console.WriteLine(crcTmp + " " + pBuffer[i] + " " + length);
            }

            return (crcTmp);
        }

Aufgerufen wird dann so:

Code:
ushort crc = crc_calculate(temp, temp.Length - 2);
if (temp.Length < 5 || temp[temp.Length - 1] != (crc >> 8) || temp[temp.Length - 2] != (crc & 0xff))
{
   //Hier schlechtes Packet
}
else
{
   //Hier gutes Packet
}

Ich lese über eine RS232 byte's ein, diese können werte von 0-255 haben :-(. Leider kann Java keine unsigned Typen.
Daher habe ich das für temp den nächst größere genommen (short).
In temp sollte also 0-255 stehen 🙂 Tut's auch.
Leider kann ich nicht die crc_calculate und crc_accumulate richtig übersetzen.

Hier meine Versuche
Code:
    private int crc_accumulate(short b, int crc)
    {
            short ch = (short)(b ^ (short)(crc & 0x00ff));
            ch = (short)(ch ^ (ch << 4));
            return (int)((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4));
    }

    private int crc_calculate(short[] pBuffer, int length)
    {
        if (length < 1)
        {
            return 0xffff;
        }
        // For a "message" of length bytes contained in the unsigned char array
        // pointed to by pBuffer, calculate the CRC
        // crcCalculate(unsigned char* pBuffer, int length, unsigned short* checkConst) < not needed

        int crcTmp;
        int i;

        crcTmp = X25_INIT_CRC;
        crcTmp = 0x0000FFFF;

        for (i = 1; i < length; i++) // skips header U
        {
        	if ( pBuffer[i] < 0)
        		pBuffer[i] = (short) (pBuffer[i] & 0xFF);
        	
            crcTmp = crc_accumulate(pBuffer[i], crcTmp);
            //Console.WriteLine(crcTmp + " " + pBuffer[i] + " " + length);
        }

        return (crcTmp);
    }
Vielleicht kann ja jemand weiterhelfen 🙂
Danke
 
Also wenns dir nicht um das Verständnis geht, könntest du einfach eine Funktion aus dem Internet nehmen.
Gibt ja genug Material.
 
Hmm, mir geht's nicht unbedingt um das Verständnis. Natürlich habe ich bereits gegoogelt,
habe aber so das rechte nicht gefunden.
Vielleicht müsste man das crc zeug doch erst mal verstehen 🙂
 

Zurück
Oben