【问题标题】:C to Lua conversion - weird resultC 到 Lua 的转换 - 奇怪的结果
【发布时间】:2017-01-24 03:18:49
【问题描述】:

我有一个想要转换为 LUA 的 C 函数,但我从 Lua 中得到了奇怪的结果:

unsigned short crc16(const char* pstrCurrent, int iCount)
{
    unsigned short wCRC = 0;
    int iIndex = 0;

    while(--iCount >= 0)
    {
        wCRC = wCRC ^ ((int)(*pstrCurrent++) << 8);
        printf ("WCRC = %u\n", wCRC);
    }
    return (wCRC & 0xFFFF);
}

这是我启动 Lua 的方式:

local function crc16(keyCurrent, byteCount)
    wCRC = 0
    byteIndex = 1
    local crcInput = {}

    while byteCount > 0 do    
        print ("BYTE COUNT= " .. byteCount)

        wCRC=bit32.bxor(wCRC, bit32.lshift(keyCurrent[byteIndex], 8))
        print ( "WCRC = " .. wCRC )

        byteCount = byteCount-1
        byteIndex = byteIndex+1

    end
end

是的,我知道 C 函数不完整,我只是想比较导致问题的原因。

WCRC 的打印是 C 和 Lua 为相同的输入打印完全不同的数字。

我的 Lua 转换不正确吗?这是我第二次或第三次使用 Lua,所以不太确定我做错了什么。

**********************更新********************

这里是完整的 C 和 LUA 以及一个快速的小测试代码:

unsigned short crc16(const char* pstrCurrent, int iCount)
{
    unsigned short wCRC = 0;
    int iIndex = 0;
    // Perform the following for each character in the buffer
    while(--iCount >= 0)
    {
        // Get the byte information for the calculation and
        // advance the pointer
        wCRC = wCRC ^ ((int)(*pstrCurrent++) << 8);
        for(iIndex = 0; iIndex < 8; ++iIndex)
        {
            if(wCRC & 0x8000)
            {
                wCRC = (wCRC << 1) ^ 0x1021;
            }
            else
            {
                wCRC = wCRC << 1;
            }
        }
    }
    return (wCRC & 0xFFFF);
}

以及 LUA 转换:

function crc16 (keyCurrent, iCount)
    wCRC = 0
    byteIndex = 1
    iIndex = 0
    local crcInput = {}

    while iCount >= 1 do
        wCRC = bit32.bxor (wCRC, bit32.lshift(keyCurrent[byteIndex], 8))
                for iIndex=0,8 do
                    if (bit32.band (wCRC, 0x8000) ~= nil ) then
                            wCRC = bit32.bxor (bit32.lshift (wCRC, 1), 0x1021)
                    else
                            wCRC = bit32.lshift (wCRC, 1)
                    end

                end
        iCount = iCount-1
        byteIndex = byteIndex+1
    end
    return (bit32.band (wCRC, 0xFFFF))
end

local dKey = {}
dKey = {8, 210, 59, 0,  18, 166, 254, 117}
print ( "CRC = " .. crc16 (dKey ,8) )

在 C 中,对于相同的数组,我得到:CRC16 = 567

在 LUA 中,我得到:CRC = 61471

谁能告诉我我做错了什么?

谢谢

【问题讨论】:

  • 两种语言的输入和输出是什么?

标签: lua


【解决方案1】:

似乎它们产生了相同的结果:

pure-C

WCRC = 18432
WCRC = 11520
WCRC = 16640
WCRC = 11520

pure-Lua

字节数= 4
WCRC = 18432
字节数= 3
WCRC = 11520
字节数= 2
WCRC = 16640
字节数= 1
WCRC = 11520

ASCII convertor:

什么意思?

【讨论】:

    【解决方案2】:

    更改的 Lua 示例存在错误。
    1. bit32.band() 返回数字。数字 0 不等于 'nil',那是完全不同的类型。您正在尝试将数字与 nil 进行比较,并且该检查将始终失败。
    2。 for iIndex=0,8 do 迭代 9 次,包括最终索引 8。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-18
      • 2021-01-29
      • 2011-10-07
      • 2019-12-10
      • 1970-01-01
      • 2016-09-16
      • 2012-09-03
      • 1970-01-01
      相关资源
      最近更新 更多