我正在尝试将以下 C 函数转换为 Ruby,但无法弄清楚我做错了什么。我感觉它与数据类型有关,特别是在将它们传递给 Ruby 函数时,但无法查明确切的错误。
这是我的 C 代码:
#include <stdio.h>
#include <stdint.h>
uint32_t btle_calc_crc(uint32_t crc_init, uint8_t *data, int len) {
uint32_t state = crc_init;
uint32_t lfsr_mask = 0x5a6000; // 010110100110000000000000
int i, j;
for (i = 0; i < len; ++i) {
uint8_t cur = data[i];
for (j = 0; j < 8; ++j) {
int next_bit = (state ^ cur) & 1;
cur >>= 1;
state >>= 1;
if (next_bit) {
state |= 1 << 23;
state ^= lfsr_mask;
}
}
}
return state;
}
int main(){
uint32_t crc_init = 0x55555555;
uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
uint32_t crc = btle_calc_crc(crc_init, data, sizeof(data));
printf("crc: %i\n", crc);
}
这是我的 Ruby 版本:
def calculate_crc(crc_init, data)
lfsr_mask = 0x5a6000
state = crc_init
data.each do |byte|
cur = byte
(0..7).each do |i|
next_bit = (state ^ cur) & 1;
cur = (cur >> 1) && 0xff # only 8 bit
state = state >> 1
if(next_bit == 1)
state = state | 1 << 23
state = state ^ lfsr_mask
end
end
end
return(state)
end
crc = calculate_crc(0x555555, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06])
puts "crc: #{crc}"