Xor all the data in the packet

I need a small program that can calculate the checksum from user input.

Unfortunately, all I know about the checksum is that it contains all the data in the packet.

I tried to find on the net an example of no luck.

I know if I have a line: 41,4D, 02,41,21,04,02,02,00,00,00,00,00,00,00,00,00,00,00,00, 00,00 , 00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00

This should result in a 6A checksum.

Hope someone can help me. If anyone has an example written in Python 3, it may also work for me

+3
source share
2 answers

If I understand "all the data in the packet" correctly, you should do something like this:

#include <iostream>
#include <vector>

using namespace std;

int main() 
{
  unsigned int data;
  vector< unsigned int > alldata;

  cout << "Enter a byte (in hex format, ie: 3A ) anything else print the checksum of previous input: ";

  while ( true )
  {
    cin >> hex >> data;

    if ( cin.fail() || cin.bad() )
        break;

    alldata.push_back( data );
    cout << "Enter a byte: ";

  }

  unsigned int crc = 0;

  for ( int i = 0; i < alldata.size(); i++ )
      crc ^= alldata[ i ];

  cout << endl << "The checksum is: " << hex << uppercase << crc << endl;

  system( "pause" );

  return 0;

}

, , 0, xor , .

EDIT: , ( , ). : , , - , "q" ( ). .

+3

:

unsigned char *packet;
unsigned char xor = 0;
for ( int i = 0 ; i < packet_len ; i ++ ) {
   xor = xor ^ packet[i];
}
// xor has the required checksum
+3

Source: https://habr.com/ru/post/1753922/


All Articles