If the request is verified as two ASCII codes, the LRC is the round-robin sum, the modulo of 256, and the complement code, that is, the checksum. If you want four digits, change the modulo value.
Two textbox are used;
Textbox1 for Character Input
Textbox2 for post-processing output
Button1 for response
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; namespace encoding {public partial class form1: FORM {public form1 () {initializecomponent ();} private void button#click (Object sender, eventargs e) {string original = textbox1.text; // read the original data from the wide text box // The data used for testing: a0030201c8 EE // start with a colon // A0 as the device address // 03 as the control instruction // 02 as the Data Length this test procedure only calculates the second bit // 01c8 as the data // EE it is the verification code string [] Buf = new string [original. length]; for (INT I = 1; I <original. length; I ++) // returns the first character of the string ":" omitted {Buf [I-1] = original. substring (I, 1);} int Len = (INT) (convert. tochar (BUF [5])-48; // read the data segment length, which is an ascii code, minus 48. it is more convenient to convert the string to decimal. // This sentence is not tested. It is used to calculate the data length of two digits: int Len = (INT) (convert. tochar (BUF [4])-48) * 10 + (INT) (convert. tochar (BUF [5])-48); int checksum = 0; For (INT I = 0; I <original. length-3; I ++) {checksum + = (INT) (convert. tochar (BUF [I]); // calculate the checksum in decimal format and} int sum = 255-checksum % 256 + 1; // calculate the checksum, LRC, ASCII verification string hex = convert. tostring (sum, 16 ). toupper (); // All ASCII characters in use are in uppercase. There are two digits in total. The principle is not much about string [] buff = new string [hex. length]; // the ASCII verification code that separates the last two digits for future comparison (INT I = 0; I