Go Down

Topic: I am trying to translate the follwing into C++ for the arduino (Read 1 time) previous topic - next topic

fdyche

The IDE does not like my return statement which I figured as much but I may be missing something else


Code: [Select]

unsigned int computeCrc(int byteArray[], int len)
{
  long crc;
  long q;
  int c;
  int i;
  crc = 0;
  for (i = 0; i < len; i++)
  {
    c = byteArray[i];
    q = (crc ^ c) & 0x0f;
    crc = (crc >> 4) ^ (q * 0x1081);
    q = (crc ^ (c >> 4)) & 0xf;
    crc = (crc >> 4) ^ (q * 0x1081);
   }
     return new byte[] { (byte)(crc & 0xff), (byte)(crc >> 8)
   }


here is the code that works in MS Visual Studio C#

Code: [Select]

public static byte[] ComputeCRC(byte[] val)
        {
            long crc;
            long q;
            byte c;
            crc = 0;
            for (int i = 0; i < val.Length; i++)
            {
                c = val[i];
                q = (crc ^ c) & 0x0f;
                crc = (crc >> 4) ^ (q * 0x1081);
                q = (crc ^ (c >> 4)) & 0xf;
                crc = (crc >> 4) ^ (q * 0x1081);
            }
            return new byte[] { (byte)(crc & 0xff), (byte)(crc >> 8) };

        }  // End of ComputeCRC
               


it is based on the X^16+X^12+X^5+1
start value = 0 and key is 0x1081 or 10201 octal

any help would be appreciated
Thanks

Nick Gammon

http://www.gammon.com.au/electronics

dxw00d

Code: [Select]
unsigned int computeCrc(int byteArray[], int len)

You tell the compiler that your function returns an unsigned int.

Code: [Select]
     return new byte[] { (byte)(crc & 0xff), (byte)(crc >> 8)

Then you try to return an array.

Nick Gammon

Pointer to an array. But you are right, the OP has modified the code that used to work.
http://www.gammon.com.au/electronics

tuxduino

Why return a byte array instead of "composing" a 16 bit value ?

AWOL

Issues with expected endianness?
Arse-about-face way of doing though.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fdyche

#6
Oct 10, 2012, 11:19 am Last Edit: Oct 10, 2012, 11:57 am by fdyche Reason: 1
Does this look like it would work then
I cant figure out how to return a byte array since coding it like C# likes as  byte[] functionname (arguemnts)  did not work
so I changed the code to place the result into globalCRC variable instead.
I would like to pass an array of any size but have not figured out how to do that so I will track the length, I need to test it
so I am going to pass it some arrays that have the proper CRC at the end.


Code: [Select]

void ComputeCRC(byte val[32] , int len)
{
 long crc;
 long q;
 byte c;
 crc = 0;
 for (int i = 0; i < len; i++)
 {
   c = val[i];
   q = (crc ^ c) & 0x0f;
   crc = (crc >> 4) ^ (q * 0x1081);
   q = (crc ^ (c >> 4)) & 0xf;
   crc = (crc >> 4) ^ (q * 0x1081);
 }
 globalCRC[0]=crc & 0xff;
 globalCRC[1]=crc >> 8;

AWOL

#7
Oct 10, 2012, 11:20 am Last Edit: Oct 10, 2012, 11:22 am by AWOL Reason: 1
Probably not.
C doesn't bounds-check array accesses.
Why not simply return a locally-defined "unsigned int"?
Using globals as a return mechanism is prehistoric.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fdyche


Probably not.
C doesn't bounds-check array accesses.
Why not simply return a locally-defined "unsigned int"?
Using globals as a return mechanism is prehistoric.


I was trying to return a unsigned int, but not sure what the code would have looked like to make the returned int had the CRC correct.
then use highbyte and lowbyte to place the 2 bytes in the array

AWOL

You can't use highByte and lowByte for that.
Code: [Select]
  return (crc & 0xff) | (crc >> 8);
might do the trick, depending on endianess.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up