开发者

Calculating a checksum for the Xor result of the data content when programming an LED board

I am programming an application to communicate with a LED indicator board. Programming language is Processing. A typical request looks like this:

<ID01><L1><PA><FE><MA><WC><FE>Probe3E<E>

where 3E is the checksum. The documentation says that the checksum "denotes the Xor Result of the data content". I'm not exactly a c开发者_运维百科oding ninja, so I can't quite figure out how to code this.

I found an allegedly working example coded in Delphi but can't transfer it to Processing:

Function SimpleCheckSum (const MyMessage : String) : byte;
Var res : byte;
    i   : integer;
Begin
  res := ord(MyMessage[1]);
  for i:=2 to length(MyMessage) do
     res := res XOR ord(MyMessage[i]);
  result := res;
End;

Any help and/or thoughts are greatly appreciated!


After more hours of research and testing the asker was able to compute the checksum correctly:

String myCommand = "<L1><PA><FE><MA><WC><FE>test"; // result should be: 62
byte res = 0;
for(int i=0; i<myCommand.length(); i++){
  res ^= byte(myCommand.charAt(i));
}
String checksum = hex(res);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜