转换一个C校验功能到Lua [英] Converting a C Checksum Function to Lua

查看:245
本文介绍了转换一个C校验功能到Lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个脚本来让我的主机设备的数据文件发送到从设备。从属需要一个校验和计算来进行,并加入到之前发送的文件(S)我的请求的结束。我的问题是,不仅我是相当新的编程,但我仍然试图全面掌握位操作。我目前在一个Java类,以便校验功能,所以功能部分确实有一个熟悉的格式,但由于我还在抓我的头位和位的库,我的翻译提供校验功能,为有问题Lua中。

I am writing a script to allow my host device to send data files to a slave device. The slave requires a checksum calculation to be made and added to the end of my requests prior to sending the file(s). My problem is that not only am I fairly new to programming, but I'm still trying to fully grasp bit manipulation. I'm currently in a Java class so the checksum function so portions of the functions do have a familiar format, but since I'm still scratching my head with bits and the bit library, I'm having problems translating the provided checksum function into Lua.

的功能,首先描述在C来提供功能之前,如下所示:

The function is first described as follows prior to be provided the function in C:

Initialise the checksum as FFFF(hex).
For each byte
    Checksum = Checksum XOR (current Byte)
    For I = 0 to 7
        If ((Checksum AND 1)=0)
            Checksum = Right_Bit_Shift Checksum 1 bit
        Else
            Checksum = (Right_Bit_Shift Checksum 1 bit) XOR A001(hex)
        Next I
    Next Byte

下面是提供的C语言编写的例子:

Here is the example provided written in C:

/*
 *Routine CRC takes a data string (data) "length" long and *
 *   * returns the checksum value
 *
 *NOTE   WORD = unsigned short int
 *       BYTE = unsigned char
 */

WORD CRC( BYTE *data, WORD length)
{
    WORD sum = 65535;  /*Checksum value initialised to FFFFhex */
    while (length-- > 0)
    {
        sum = ByteCRC(sum, *data++ );
    }
    return sum;
}

WORD ByteCRC( WORD sum, BYTE data)
{
    WORD i;  /* Loop counter */
    sum = sum ^ (WORD)data;
    for (i=0; i<8; i++)
    {
        if ( (sum&1) == 0)
            sum = sum>>1
        else
            sum = (sum >> 1) ^ 0xA001;
    }
    return sum;
}

现在,我目前就读在学校中间Java类。随着中说,这些功能有些部分我明白了。但正如我之前说的,我还没有为了写这些功能伸到Lua的格式完全了解位操作的某些方面。我试图重新创建它自己在Lua,但不相信我是正确的或关闭。不幸的是,我没有理解预期的效果不够好,甚至尝试测试了这一点。下面是我自己写的基础上提供的信息,我已经试过:

Now I'm currently enrolled in an intermediate Java class at school. With that said, some portions of these functions I understand. But as I said before, I'm still not fully understanding some aspects of bit manipulation in order to write these functions out into Lua format. I'm trying to recreate it myself in Lua but don't believe I'm correct or close. Unfortunately, I do no understand the desired effect well enough to even try testing this out. Here is what I've tried to write myself based on the information provided:

Function CRC(data, length)
    sum = 65535
    while length > 0 do
        sum = ByteCRC(sum, data=data+1)
        length = length - 1
    end
return sum
end

Function ByteCRC(sum, data)
    sum = sum ~ data
    for i = 0, 8 do
        if ((sum & 1) = 0)
            sum = sum >> 1
        else
            sum = (sum >> 1) ~ string.char(0xA001)
        end
    end
    return sum
end

任何批评和帮助我进一步的援助)写这更好的和B)找到我的翻译错误就是我要找的。

Any critiquing and further assistance in helping me to A) write this better and B) find any errors in my translation is what I'm looking for.

推荐答案

由于LUA有位操作是一样的基本概念。在这里,它是如何去,用一些code注释:

Since lua have bit operations it is the same basic concept. Here how it goes, with some code comments:

function CRC(data, length)
    sum = 65535
    local d
    for i = 1, length do
        d = string.byte(data, i)    -- get i-th element, like data[i] in C
        sum = ByteCRC(sum, d)
    end
    return sum
end

function ByteCRC(sum, data)
    sum = sum ~ data
    for i = 0, 7 do     -- lua for loop includes upper bound, so 7, not 8
        if ((sum & 1) == 0) then
            sum = sum >> 1
        else
            sum = (sum >> 1) ~ 0xA001  -- it is integer, no need for string func
        end
    end
    return sum
end

print(CRC("foo", 3));

这篇关于转换一个C校验功能到Lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆