?
Solved

Bitwise XOR of all characters in string

Posted on 2006-10-25
6
Medium Priority
?
1,343 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

862 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