Solved

Bitwise XOR of all characters in string

Posted on 2006-10-25
6
1,259 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 28

Accepted Solution

by:
pepr earned 500 total points
Comment Utility
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 28

Expert Comment

by:pepr
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Expert Comment

by:Exceter
Comment Utility
>> 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
Comment Utility
Sorry for the double negative... I meant to say, "ending program execution."
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now