Can someone convert this C code to PB?

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Can someone convert this C code to PB?

Post by swhite »

Code: Select all

static void LCPUpdateCRC(unsigned short *crc,unsigned char byte)
{

// Local variables.

    char i;                             // loop variable
    char XORFlag;                       // flag indicating the polynomial should be XORed

// Combine the new data byte with the current CRC.

    if (crc != NULL) {                  // ensure the CRC is wanted
        for (i=7; i >= 0; --i) {        // loop through each bit in the data byte
            XORFlag = (unsigned char)((*crc & 0x8000) != 0x0000);
            *crc <<= 1;                 // shift the CRC left
            *crc |= (unsigned short)((byte >> i) & 0x01);
            if (XORFlag)                // check for CRC overflow
                *crc ^= 0x1021;         // XOR with polynomial
        }
    }

Simon White
dCipher Computing
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Can someone convert this C code to PB?

Post by infratec »

Code: Select all

Procedure LCPUpdateCRC(*crc.Unicode, byte.a)

  Protected i.i         ; loop variable
  Protected XORFlag.a   ; flag indicating the polynomial should be XORed

  ; Combine the new Data byte With the current CRC.

  If *crc                       ; ensure the CRC is wanted
    For i = 7 To 0 Step -1     ; loop through each bit in the Data byte
      XORFlag = *crc\u & $8000
      *crc\u << 1               ; shift the CRC left
      *crc\u | ((byte >> i) & $01)
      If XORFlag                ; check For CRC overflow
        *crc\u ! $1021          ; XOR with polynomial
      EndIf
    Next i
  EndIf
  
EndProcedure
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Can someone convert this C code to PB?

Post by swhite »

Thank-you for your help. I was having difficulty figuring out what

Code: Select all

*crc |= (unsigned short)((byte >> i) & 0x01)
was actually doing. Now I see that they were starting with MSB and "ORing" it with the CRC and working down to the LSB.

Simon
Simon White
dCipher Computing
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Can someone convert this C code to PB?

Post by swhite »

infratec wrote:

Code: Select all

Procedure LCPUpdateCRC(*crc.Unicode, byte.a)

  Protected i.i         ; loop variable
  Protected XORFlag.a   ; flag indicating the polynomial should be XORed

  ; Combine the new Data byte With the current CRC.

  If *crc                       ; ensure the CRC is wanted
    For i = 7 To 0 Step -1     ; loop through each bit in the Data byte
      XORFlag = *crc\u & $8000
      *crc\u << 1               ; shift the CRC left
      *crc\u | ((byte >> i) & $01)
      If XORFlag                ; check For CRC overflow
        *crc\u ! $1021          ; XOR with polynomial
      EndIf
    Next i
  EndIf
  
EndProcedure
The XORFlag should be at least a 2 byte type or it will always be zero and the code will not work.

Simon
Simon White
dCipher Computing
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Can someone convert this C code to PB?

Post by infratec »

The char type in the C code ...

Or you can use

Code: Select all

XORFlag = bool((*crc\u & $8000) <> 0)
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Can someone convert this C code to PB?

Post by swhite »

Yes I just changed the XORflag to an integer and everything works fine and gives the expected results when compared with the supplied test data. So thanks again for your help.

Simon
Simon White
dCipher Computing
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Can someone convert this C code to PB?

Post by wilbert »

Isn't this the same checksum you were working on before ?
http://www.purebasic.fr/english/viewtop ... =7&t=59744
The polynomial seems to be the same.
Windows (x64)
Raspberry Pi OS (Arm64)
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Can someone convert this C code to PB?

Post by swhite »

Hi

I thought so but when I compared the results they were not the same. It seems that each equipment manufacturer has a slightly different variation on how they implement their CRCs. So now I have about 5 or 6 different CRC calculations.

Simon
Simon White
dCipher Computing
Post Reply