Solved

Bitwise XOR of all characters in string

Posted on 2006-10-25
6
1,287 Views
Last Modified: 2012-06-22
I'm trying to read NMEA gps signal from COM1, modifying it and sending it out on COM2. In this case I need to check the chekcsum of the incoming signal, and generate a checksum for the new modified data stream.

The incoming data is like this
$abc,def,ghi,jkl*FF
I need to do this
a XOR b XOR c XOR , XOR d  (...) XOR k XOR l

i.e. XOR all characters between $ and *
Then the checksum is the hexadesimal value typed at the end of the string e.g. FF

What is the fastest and most efficient way of doing this?   I don't mind putting ASM code in the project if that will be the most efficient and elegant.

Arild
0
Comment
Question by:arildj78
  • 3
  • 2
6 Comments
 
LVL 29

Accepted Solution

by:
pepr earned 500 total points
ID: 17806135
This is probably only part of the answer. Specify better your question

#include <string>
#include <iostream>

int main()
{
    using namespace std;
    string s("$abc,def,ghi,jkl*");

    int sum = 0;

    for (string::size_type i = 1; i < s.length() - 1; ++i)
    {
        sum ^= s[i];  // equal to sum = sum ^ s[i];  i.e. XOR
    }

    cout << "'" << s << hex << sum << "'" << endl;

    return 0;
}
0
 
LVL 29

Expert Comment

by:pepr
ID: 17806151
This kind of checksum is not reliable. Try CRC instead. Have a look at http://cpp.snippets.org/code/crc.txt -- A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS by Ross N. Williams.

The sources can be found at http://cpp.snippets.org/code/
as crc.cpp, crc.hpp, crc.h, crc_16.c, crc_16f.c, crc_32.c (depends what you need and like).
0
 
LVL 2

Author Comment

by:arildj78
ID: 17806431
I'm all new to C++ but this seems to solve my XOR problem as stated in my question. The rest of the program will probably show up here as questions ;)

I've read a bit about checksums and am aware of the weaknesses of this way of doing it, but this is the way that is speciefied in the NMEA-0183 protocol commonly used in GPS to RS-232 communications.

I will post again as soon as I've tested the code.


Can you explain how the code works?
                                              for (string::size_type i = 1; i < s.length() - 1; ++i)
                                              {
                                                  sum ^= s[i];  // equal to sum = sum ^ s[i];  i.e. XOR
                                              }

                                              cout << "'" << s << hex << sum << "'" << endl;

                                              return 0;
I'm not to sure how this works :(   If you don't have time, that's ok.  I will probably find out anyway  :)

Arild
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 8

Expert Comment

by:Exceter
ID: 17807263
>> Can you explain how the code works?

>> for (string::size_type i = 1; i < s.length() - 1; ++i)
Initiates a for loop with a counter names i of type string::size_type. i is incremented once for every loop iteration. The loop terminates when i is greater than or equal to the length of the string.

>> sum ^= s[i];  // equal to sum = sum ^ s[i];  i.e. XOR
The ^= operator takes the left operand, which is the integer variable sum, XORs it against the right operand, which is the character s[i], and stores the result in the integer variable sum.

>> cout << "'" << s << hex << sum << "'" << endl;
Displays the string s followed by the integer sum encapsulated by single quote marks.

>> return 0;
Returns 0 and ends function execution. As this is in the main function, this also has the effect of ending program termination.

Cheers!
Exceter
0
 
LVL 8

Expert Comment

by:Exceter
ID: 17807272
Sorry for the double negative... I meant to say, "ending program execution."
0
 
LVL 29

Expert Comment

by:pepr
ID: 17855966
Arildj78: Well, I know nothing about the NMEA-0183 protocol. It is likely that the XOR is used because it is an extremely easy operation for even the simplest processor. There probably is no faster operation. The program that I wrote mainly demonstrates the XOR operation itself. Also, the string is processed from the second character (zero based indexing and index 1) to skip the $ and it does not include the last character '*'. It also does not assume here that you could receive the sum on the input to check it. The result is only displayed, not converted and appended to the string.

The most magical could be the
cout << "'" << s << hex << sum << "'" << endl;

Think about it as about the print command that displays the string and the sum.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question