问题
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.
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
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;
}
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
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.
回答1:
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));
来源:https://stackoverflow.com/questions/34120322/converting-a-c-checksum-function-to-lua