Pages: [1]   Go Down
Author Topic: I am trying to translate the follwing into C++ for the arduino  (Read 940 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 
Code:
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:
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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question

And the error message was?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Then you try to return an array.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Pointer to an array. But you are right, the OP has modified the code that used to work.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25797
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Issues with expected endianness?
Arse-about-face way of doing though.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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;
« Last Edit: October 10, 2012, 04:57:43 am by fdyche » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25797
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 10, 2012, 04:22:26 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25797
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't use highByte and lowByte for that.
Code:
  return (crc & 0xff) | (crc >> 8);
might do the trick, depending on endianess.
Logged

"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.

Pages: [1]   Go Up
Jump to: