我想把MSSQL语言的binary_checksum从c#翻译成Lua,但我是Lua的新手...
private static int SQLBinaryChecksum(string text)
{
long sum = 0;
byte overflow;
for (int i = 0; i < text.Length; i++)
{
sum = (long)((16 * sum) ^ Convert.ToUInt32(text[i]));
overflow = (byte)(sum / 4294967296);
sum = sum - overflow * 4294967296;
sum = sum ^ overflow;
}
if (sum > 2147483647)
sum = sum - 4294967296;
else if (sum >= 32768 && sum <= 65535)
sum = sum - 65536;
else if (sum >= 128 && sum <= 255)
sum = sum - 256;
return (int)sum;
}发布于 2018-03-29 22:25:44
您没有指定您的Lua版本,所以我建议您使用LuaJIT。
-- Works only on LuaJIT
function SQLBinaryChecksum(text)
local sum = 0
for i = 1, #text do
sum = bit.bxor(bit.rol(sum, 4), text:byte(i))
end
if sum >= 32768 and sum <= 65535 then
sum = sum - 65536
elseif sum >= 128 and sum <= 255 then
sum = sum - 256
end
return sum
end
-- Did you notice that Lua code is shorter than C#?如果文本包含ASCII以外的字符,则此代码可能无法正常工作。
要解决这个问题,我应该知道字符串€的正确校验和
https://stackoverflow.com/questions/49557760
复制相似问题