C # Bcc XOR checksum method

Source: Internet
Author: User

The last one in the communication protocol is the check digit.

Adopt BCC(Xor check) method

How do you explain this?

is to convert a 16 binary string into byte[] data, then take it out for an XOR and then return the value and check the comparison to see if it's right

For example:

232301fe313233343536373839303132333435363701001e0f01021226270001383938363032623531333136353033393731363001009e

The last one is 9E decimal for 158

Then we'll intercept the front part of 9E.

232301fe313233343536373839303132333435363701001e0f0102122627000138393836303262353133313635303339373136300100

Incoming method

<summary>
BCC and checksum code
</summary>
<param name= "Data" > packets that need to be validated </param>
<returns></returns>
Public byte Get_checkxor (byte[] data)
{
byte Checkcode = 0;
int len = data. Length;
for (int i = 0; i < len; i++)
{
Checkcode ^= Data[i];
}
return checkcode;
}

Return the same as the result is 158

If you haven't read it yet, we'll go back to 16.

<summary>
BCC and checksum code return 16 binary
</summary>
<param name= "Data" > packets that need to be validated </param>
<returns></returns>
public string Getbccxorcode (byte[] data)
{
byte Checkcode = 0;
int len = data. Length;
for (int i = 0; i < len; i++)
{
Checkcode ^= Data[i];
}
Return convert.tostring (Checkcode, 16). ToUpper ();
}

Return 9E

Learned a morning decided to share under the hope can help to the newly contacted classmates

C # bcc XOR checksum

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.