I am very new to Ruby, please help.
This is a C code which I need to convert into Ruby.
Passing values [1,2,3,4,5] it gives me B059 in HEX
unsigned short CalcCrc16(const unsigned char *Data,unsigned short DataLen)
{
unsigned short Temp;
unsigned short Crc;
Crc = 0;
while (DataLen--)
{
Temp = (unsigned short)((*Data++) ^ (Crc >> 8));
Temp ^= (Temp >> 4);
Temp ^= (Temp >> 2);
Temp ^= (Temp >> 1);
Crc = (Crc << 8) ^ (Temp << 15) ^ (Temp << 2) ^ Temp;
}
return Crc;
}
This is the Ruby code I have tried:
class CRC16
def CRC16.CalculateCrc16(data)
crc = 0x0000
temp = 0x0000
i = 0
while i < data.Length
value = data[i]
temp = (value ^ (crc >> 8))
temp = (temp ^ (temp >> 4))
temp = (temp ^ (temp >> 2))
temp = (temp ^ (temp >> 1))
crc = (((crc << 8) ^ (temp << 15) ^ (temp << 2) ^ temp))
i += 1
end
return crc
end
end
Please help me to convert this code into Ruby. Thanks Deepak