Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
Heyy Folks!

Please explain me that code n rewrite in C# (checksum code)

C++
byte crc8(byte *str, int len)
{
  int i, f;
  byte data;
  byte crc;
 
  crc = 0 ;
  while (len--) 
  {
    data = *str++ ;
    for = (i = 0; i < 8; i++) 
    {
      f = 1 & (data ^ crc);
      crc >>= 1;
      data >>= 1 ;
    
      if (f) 
      {
        crc ^= 0x8c;
      }
    }
  }
 
  return crc;
}
Posted
Updated 28-Jul-14 18:43pm
v4
Comments
PIEBALDconsult 29-Jul-14 0:21am    
I don't think that's a proper CRC algorithm. Where did you get it?
Rewriting it in C# should be fairly trivial because only the array access parts should need to be changed.
Sergey Alexandrovich Kryukov 29-Jul-14 0:47am    
Not clear what are you asking about. What are you trying to achieve. Is it your code or not? In what language is the code you are showing? Why would you ever need an explanation of some code? Write your code, show it and ask a question is you have any problems.
Don't use pointers on .NET without special need. Use array of bytes in this case, byte[].
—SA
PIEBALDconsult 29-Jul-14 0:54am    
It looks like he got this in C/C++ and wants it translated, but doesn't know either language.
Translating the code to C# is simple. Translating to English is pretty much impossible.
Sergey Alexandrovich Kryukov 29-Jul-14 0:55am    
Agree. I suggested some basic steps for such translation in my answer, it should be quite enough.
—SA
PIEBALDconsult 29-Jul-14 1:00am    
I assume it's some convoluted security-through-obscurity scheme that some other developer on his project devised. It looks very very bad.

Please see my comment to the question.

Instead of that byte pointer, use the array type byte[] and call this parameter data, which is not strings. Strings in .NET are Unicode strings, not arrays of bytes (a character is not a byte), and not arrays at all. So, use the array, and, instead of using the pointer increment, use the cycle for (int index = 0; index < data.Length; ++index), then the array element will be data[index].

Everything else will be pretty much the same. And use the debugger in the case of even a slightest doubt of your runtime.

Good luck.
—SA
 
Share this answer
 
Comments
Andreas Gieriet 29-Jul-14 1:36am    
My 5! Now the (next) challenge is probably to get a byte stream from text data... ;-)
Cheers
Andi
Sergey Alexandrovich Kryukov 29-Jul-14 4:27am    
Thank you Andi. Oh yes, such a challenge... :-)
Good point though. For OP: use System.Text.Encoding types.
—SA
For an intro on CRC see http://en.wikipedia.org/wiki/Cyclic_redundancy_check[^].
Try to match the code with the example shown in the link above.
Cheers
Andi
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Jul-14 4:28am    
Makes sense, 5ed.
—SA
Andreas Gieriet 29-Jul-14 10:41am    
Thanks for your 5!
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900