我对Ruby很陌生,请帮忙。
这是我需要转换成 Ruby 的 C 代码。
传递值 [1,2,3,4,5] 它给了我十六进制的 B059
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;
}
这是我尝试过的 Ruby 代码:
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
请帮我将此代码转换为 Ruby。谢谢迪帕克