lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Wed, Oct 23, 2019 at 10:08 PM Jonathan Goble <jcgoble3@gmail.com> wrote:
On Thu, Oct 24, 2019 at 12:03 AM Russell Haley <russ.haley@gmail.com> wrote:
>
> Results:
>
> C:\Users\russh\projects\NLuaSerialScripts> lua .\test-crc.lua
> Lua Calling C hash:
> -16032.0
> C on packed string
> -16032.0
> Lua Clark Li:
> Disabled, my variant didn't work
> Lua converted C:
> 49504
>
> Any ideas why I get a different number? All input greatly appreciated.
>
> Russ

You're actually getting the same number. The difference is that the
original C code is using signed 16-bit integers and 49504 has an MSB
of 1, so two's complement is used, while Lua is using 64-bit integers,
so two's complement doesn't come into play (MSB is 0). If you put
49504 into an unsigned 16-bit integer in C and then cast it to signed,
the result will be -16032.

Ah ha! Thank you, Johnathan, crc & 0xffff solved it. I'll work on the Li variant tomorrow. 

Regards, 
Russell