title: Convert crc8 function in C to Java
body: <p>I received a CRC function written in C from a hardware partner. Messages send by his devices are signed using this code. Can anyone help me to translate it to Java?</p>
  
  <pre><code>int8 crc8(int8*buf, int8 data_byte)
  {
      int8 crc=0x00;
      int8 data_bit=0x80;
      while(data_byte&gt;0)
      {
          if(((crc&amp;0x01)!=0)!=((buf[data_byte]&amp;data_bit)!=0))
          {
              crc&gt;&gt;=1;
              crc^=0xCD;
          }
          else 
              crc&gt;&gt;=1;
          data_bit&gt;&gt;=1;
          if(!data_bit)
          {
              data_bit=0x80;
              data_byte--;
          }
      }
      return(crc);
  }
  </code></pre>
  
  <p>I tried to convert this to Java, but the result is not I expect.</p>
  
  <pre><code>public static byte crc8(byte [] buf, byte data_byte)
  {
    byte crc = 0x00;
    byte data_bit = (byte)0x80;
    while(data_byte&gt;0)
    {
      if(((crc&amp;0x01)!=0)!=((buf[data_byte]&amp;data_bit)!=0))
      {
        crc&gt;&gt;=1;
        crc^=0xCD;
      }
      else
      {
        crc&gt;&gt;=1;
      }
      data_bit&gt;&gt;=1;
      if(data_bit == 0)
      {
        data_bit= (byte)0x80;
        data_byte--;
      }
    }
    return crc;
  }
  </code></pre>
  
  <p>I suppose that this is the error: <code>if(data_bit != 0)</code></p>
  
  <p>EDIT:</p>
  
  <p>I changed the code to byte in my conversion method. I receive my data from a socket and convert this then to a String where I get a byteArray out from. </p>
  
  <p>An input example is 16, 0, 1, -15, 43, 6, 1, 6, 8, 0, 111, 0, 0 ,49
  where the last field (49) should be the checksum</p>
  
  <p>I also tried Durandals version, but my result is still not valid.</p>
  
  <p>This is how I read the data</p>
  
  <pre><code>BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  char[] buffer = new char[14];
  int count= bufferedReader.read(buffer, 0, 14); 
  String msg = new String(buffer, 0, count);
  byte[] content = msg.getBytes();
  </code></pre>