Go Down

### Topic: C# bitwise operation -> CRC8 calculation (Read 1 time)previous topic - next topic

#### Unictu

##### Nov 24, 2017, 08:31 am
Hi everyone,

After learning Arduino, then a bit of C in very short period, now I found myself testing out C#.  Hope you can shed a light on my current issue.

I stuck with converting a Arduino sub-function which return CRC8-Maxim value of a array of bytes to C#. I notice C# bitwise operation is very different to C and is difficult.

Arduino codes:
Code: [Select]
`        byte CRC8(byte len)        {            // This function calculate and return CRC8 of data bytes            // CRC-8 - based on the CRC8 formulas by Dallas/Maxim            // http://www.maxim-ic.com/appnotes.cfm/appnote_number/27            byte crc = 0x00;            for (byte i = 0; i < len; i++)            {                byte extract = bytes1ToSend[i]++; for (byte n = 8; n > 1; n--) {      byte sum = (byte)((crc ^ extract) & 0x01);                     crc >>= 1;      if ((int)sum == 1)      {   crc ^= 0x8C;      }                     extract >>= 1; }     }         return crc;        }`

As far as I went with C#

Code: [Select]
`        byte CRC8(byte len)        {            // This function calculate and return CRC8 of data bytes            // CRC-8 - based on the CRC8 formulas by Dallas/Maxim            // http://www.maxim-ic.com/appnotes.cfm/appnote_number/27            byte crc = 0x00;            for (byte i = 0; i < len; i++)            {                byte extract = bytesToSend[i]++; for (byte n = 8; n > 1; n--) {               byte sum = (byte)((crc ^ extract) & 0x01);                      crc >>= 1;       if ((int)sum == 1)       {              crc ^= 0x8C;       }                      extract >>= 1;         }     }     return crc;        }`

It did not work

Thanks for any suggestion.

#### Robin2

#1
##### Nov 24, 2017, 10:36 am
Isn't that a question for a C# Forum ?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### el_supremo

#2
##### Nov 24, 2017, 05:26 pm
Quote
I notice C# bitwise operation is very different to C and is difficult
The website I found about bitwise operators in C# shows that they are exactly the same as C, in which case they aren't difficult at all.

Quote
It did not work
Not surprising. The "Arduino codes" don't work either. Where did you get that code from?

Pete
Don't send me technical questions via Private Message.

#### Unictu

#3
##### Nov 25, 2017, 02:47 am
Isn't that a question for a C# Forum ?

...R
I wasn't sure about category. Anyway, wonder if a Mod can help move it. Thank you.

#### Unictu

#4
##### Nov 25, 2017, 03:06 am
The website I found about bitwise operators in C# shows that they are exactly the same as C, in which case they aren't difficult at all.
Not surprising. The "Arduino codes" don't work either. Where did you get that code from?

Pete

As a agriculture mechanical eng. everything in this field is difficult to me. To be honest, I can do ok with Arduino after a year or two tinkering. With other languages, especially C# which I have just started 2 days ago, it was a bit too much. That's where I am atm.

I was trying to replicate same operation for C# but found out byte array not supported so there seems to be no simple way to get it work. I tried to learn related C# information yesterday afternoon. I will keep trying, like right now on Sat afternoon, but hope someone can help me to the right direction. You Don't Know What You Don't Know.

The website I found about bitwise operators in C# shows that they are exactly the same as C, in which case they aren't difficult at all.
Not surprising. The "Arduino codes" don't work either. Where did you get that code from?

Pete

You are right, that Arduino code did not work. It was probably one of many attempts I was trying for C#. My mind got stuffed up on Friday afternoon sorry.

Original code from http://www.leonardomiliani.com/en/2013/un-semplice-crc8-per-arduino/

Code: [Select]
`//CRC-8 - based on the CRC8 formulas by Dallas/Maxim//code released under the therms of the GNU GPL 3.0 licensebyte CRC8(const byte *data, byte len) {  byte crc = 0x00;  while (len--) {    byte extract = *data++;    for (byte tempI = 8; tempI; tempI--) {      byte sum = (crc ^ extract) & 0x01;      crc >>= 1;      if (sum) {        crc ^= 0x8C;      }      extract >>= 1;    }  }  return crc;}`

Once again, appreciate any help.

#### el_supremo

#5
##### Nov 25, 2017, 03:24 am
So your question has nothing to do with bitwise operations.

There are no C# forums here so there's nowhere more suitable for a mod to move this thread. You should find a forum somewhere on the web that handles C# questions.

Pete
Don't send me technical questions via Private Message.

Go Up