Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用截断多项式800516将CRC16从C转换为Ruby_C_Ruby_Crc16 - Fatal编程技术网

使用截断多项式800516将CRC16从C转换为Ruby

使用截断多项式800516将CRC16从C转换为Ruby,c,ruby,crc16,C,Ruby,Crc16,我对Ruby很陌生,请帮忙 这是一个C代码,我需要将其转换为Ruby 传递值[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--) {

我对Ruby很陌生,请帮忙

这是一个C代码,我需要将其转换为Ruby

传递值[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; 
   }
unsigned short CalcCrc16(常量unsigned char*Data,unsigned short DataLen)
{
无符号短时间;
无符号短Crc;
Crc=0;
while(DataLen--)
{
Temp=(无符号短)((*数据++)^(Crc>>8));
温度^=(温度>>4);
温度^=(温度>>2);
温度^=(温度>>1);
Crc=(Crc>4))
温度=(温度^(温度>>2))
温度=(温度^(温度>>1))
crc=((crc你就快到了

以下是修复方法:

class CRC16 
  def CRC16.CalculateCrc16(data) 
    crc = 0
    temp = 0
    i = 0 
    while i < data.length    # Modified from OP version
      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))
      crc &= 0xffff          # New - keep integer in "unsigned short" bit space
      i += 1 
    end 
    return crc 
  end 
end
CRC16级
def CRC16.计算ECRC16(数据)
crc=0
温度=0
i=0
而i>8))
温度=(温度^(温度>>4))
温度=(温度^(温度>>2))
温度=(温度^(温度>>1))
crc=((crc你就快到了

以下是修复方法:

class CRC16 
  def CRC16.CalculateCrc16(data) 
    crc = 0
    temp = 0
    i = 0 
    while i < data.length    # Modified from OP version
      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))
      crc &= 0xffff          # New - keep integer in "unsigned short" bit space
      i += 1 
    end 
    return crc 
  end 
end
CRC16级
def CRC16.计算ECRC16(数据)
crc=0
温度=0
i=0
而i>8))
温度=(温度^(温度>>4))
温度=(温度^(温度>>2))
温度=(温度^(温度>>1))

crc=(((crc你尝试过什么?如果你至少没有展示你尝试过什么或者你的具体问题在哪里,没有人会为你做这些工作。你尝试过什么?如果你至少没有展示你尝试过什么或者你的具体问题在哪里,没有人会为你做这些工作。
crc&=0xffff
在C版本中添加以处理这些问题是有意义的使用variant
short
size.(uint32\u t更好)非常感谢您…我从您的解决方案中得到了答案…:)如果我想通过“12345”来代替[1,2,3,4,5],再查询一次。以获得相同的o/p。您能帮助我在代码中的何处更改以获得所需的结果b059吗。@Deepak Kumar Jha:字符
“1”
与数字
1
不同,您确定要这样做吗?您的输入是否真的包含一个值为0到9的数组?如果是这样,Ruby one liner将是
“12345”.chars.map{c | c.to _i}
或者更一般地说
numbers=str.chars.map{c | c.to _i}
其中
str
是您的输入字符串。
crc&=0xffff
在C版本中添加变量
短的
大小是有意义的。(uint32\u t更好。)非常感谢您…我从您的解决方案中得到了我的答案…:)如果我想通过“12345”来代替[1,2,3,4,5],请再查询一次。以获得相同的o/p。能否请您帮助我在代码中的何处更改以获得所需的结果b059。@Deepak Kumar Jha:
“1”
字符与
1
数字不同,您确定要这样做吗?您的输入是否真的包含一个值为0到9的数组?如果是,Ruby one liner将是
“12345”。chars.map{| c | c.to_i}
或更一般的
numbers=str.chars.map{| c | c.to_i}
其中
str
是您的输入字符串。