Solved

Bitwise XOR of all characters in string

Posted on 2006-10-25
6
1,297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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