I have a checksum that I need to add to a ruby string in hex. I have been unable to convert the checksum successfully. I am relatively new to ruby, so I’m not sure if I’m missing something. Here is what I am doing:
def get_checksum message
# get the checksum
cnt = 0
lrc = 0
while (cnt < message.length - 1)
lrc = lrc ^ message[cnt].to_i
cnt += 1
end
# return as hex
lrc.to_s.each_byte.map { |b| b.to_s(16) + " " }.join
end
I have some c# reference code as well, but have never used C# being a long time mac C/C++/Obj-C coder. Here is the C# code I am trying to convert:
// calculate LRC
private string GetChecksum(string inputstring)
{
int checksum = 0;
foreach (char c in inputstring)
{
checksum ^= Convert.ToByte(c);
}
return checksum.ToString("X2");
}
Any help would be appreciated.
.to_iwill return0when called on a character.