Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I got this, which is not working. Tried printing both strings with cout, and they look exactly the same. tried str.find, strcmp also.
C++
string MyWebResponse = client.GetResponse();
string hash = HASH(token + time + user + password + salt);
/*
MyWebResponse = 5accdd72bb566079faf459cbad0934c5
and
hash          = 5accdd72bb566079faf459cbad0934c5

tho, I can't get my program to recognize they're exactly the same

*/
if(MyWebResponse == hash){
    //Do something, but it never gets there.
}
Posted
Updated 25-Sep-15 0:38am
v2
Comments
PIEBALDconsult 24-Sep-15 21:10pm    
:cough: Reference comparison. :cough:
Afzaal Ahmad Zeeshan 25-Sep-15 0:57am    
If only he knew it.
CPallini 25-Sep-15 2:50am    
Nope.
The == operator uses compare, which, in turns, performs the comparison on the sequence of the contained characters.

Of course the '==' operator does work. That means you are not actually observing the correct string values. As pointed out by George Swan there could be spurious control characters in one (or both) of them, causing the mismatch.
try, for instance
 #include <iostream>
 #include <string>
using namespace std;

void dump( string s)
{
  for (unsigned int n=0; n<s.length(); ++n)
  {
    char c = s[n];
    cout << (int) c << ",";
  }
  cout << endl;

}

int main()
{

  string s1 = "asaaaa\r";
  string s2 = "asaaaa\n";
  cout << s1 << endl;
  cout << s2 << endl;

  dump(s1);
  dump(s2);
}
 
Share this answer
 
v2
Comments
daer trea 25-Sep-15 15:38pm    
50,55,101,50,53,100,102,54,50,52,101,53,98,57,53,52,53,50,49,54,54,55,54,100,48,
55,50,53,56,55,51,52, <-- hashed one

50,55,101,50,53,100,102,54,50,52,101,53,98,57,53,52,53,50,49,54,54,55,54,100,48,
55,50,53,56,55,51,52,32, <-- reponse one (Has an extra random space character it seems)

Thanks, it works fine now that I make it remove that space.
CPallini 25-Sep-15 15:57pm    
You are welcome.

You have probably got control characters in one of your strings. Try cleaning them up by doing something this.


C#
string MyWebResponse = "5accdd72bb566079faf459cbad0934c5";
string cleanedResponse=  new string(MyWebResponse.Where(c=>!char.IsControl(c)).ToArray());
 
Share this answer
 
Comments
CPallini 25-Sep-15 2:54am    
"You have probably got control characters in one of your strings"
That really makes sense. Have my 5.
Please note (bare) C++ has no LINQ.
Try strcmp instead.
 
Share this answer
 
Comments
daer trea 24-Sep-15 21:43pm    
Already tried
if(strcmp(MyWebResponse.c_str(), hash.c_str() == 0){
//Do something
}
but no success

Both strings result are: (Separated with a new line)
https://gyazo.com/37e52c36d8c03ddbd93a64dc4ed8c1ef
CPallini 25-Sep-15 2:54am    
I see a "\n0" in the second one.
daer trea 25-Sep-15 15:28pm    
that's from other code
 
Share this answer
 
A few things to consider before actually answering the question. First of all, I hope you are aware that string is not an actual type, it is just a representation of array of characters. In high-level programming you are not shown what the data is like underground, so these types of "string" or other similar come into action. But each of the framework has built-in functions to find out more.

Now the code that you've shown, is more like C# type (client.GetResponse()? That is more like an object from C# being called there; based on the naming convention, may be wrong). But, in C++ a string can be compared to another string using == operator. What happens is that the strings are compared using std::compare[^] to determine whether both are same or not and returns a value indicating true or false. (String is not an actual type!)

Your code, I have tested and works just perfect. I tried the following code,

C++
#include <iostream>
#include <string>

int main()
{
    std::string webresponse = "5accdd72bb566079faf459cbad0934c5";
    std::string secondStr = "5accdd72bb566079faf459cbad0934c5";

    if(webresponse == "5accdd72bb566079faf459cbad0934c5" && 
       webresponse == secondStr) { // In case you think variable may cause a trouble
        std::cout << "Equal";
    } else {
        std::cout << "Not equal";
    }
    
    return 0;
}

// Output:
// Equal


You can test it right here, http://cpp.sh/8rdi[^]

Now the thing that I wanted to point out is that are you sure it doesn't work? I am sure C# would also work in the same way if that is a C# snippet. However, the answer is inpired by PIEBALDconsult's comment to your question, the actual answer is what he wrote there. :-)
 
Share this answer
 
v2
Comments
CPallini 25-Sep-15 2:52am    
"What happens is that the strings are compared based on their reference"
In C++ this is plain wrong.
See http://www.cplusplus.com/reference/string/string/operators/
Afzaal Ahmad Zeeshan 25-Sep-15 10:24am    
Oh, my bad. In real the questions make very less sense if viewed from a C++ point of view but seems more legit if viewed from C# point of view. Perhaps a wrong tag and title.

But, thanks for heads up. Removed the ambiguity. Hope that suffice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900