Page 1 of 1

compare unity8_t to a string not working

Posted: Tue Nov 29, 2016 7:20 pm
by Canedje
I do try to test an unity8_t var (payload) with a string (p).

I do convert a string to an unity8_t var called p, by:

Code: Select all

String test = "message received : Ben je er al?";
const uint8_t* p = reinterpret_cast<const uint8_t*>(test.c_str());
Then I do compare "p" with a websocket called "payload" by:

Code: Select all

if ( payload == p) {
If I print both p and payload I do see no difference but the if test is false?

How is this possible?
What am I doing wrong?

Re: compare unity8_t to a string not working

Posted: Tue Nov 29, 2016 7:59 pm
by PeterO
Do you understand what a pointer is ?

PeterO

Re: compare unity8_t to a string not working

Posted: Tue Nov 29, 2016 8:04 pm
by Canedje
yes I do.
In the past I did programming. That is a while ago.

Re: compare unity8_t to a string not working

Posted: Tue Nov 29, 2016 8:34 pm
by PeterO
Canedje wrote:yes I do..
Are you sure ? You are comparing pointers to two strings and wondering why they aren't the same. You need to compare the strings themselves, not their addresses.

PeterO

Re: compare unity8_t to a string not working

Posted: Tue Nov 29, 2016 9:08 pm
by Canedje
Thanks.
I know that a pointer is an adress to the value and not the value itself.
I still having trouble comparing a uint8_t with a string.

I do try to compare the uint8_t var named "payload" with a string named "test"
If both the value is : "Ben je er al?" the if statement is true.
I don't know how the codse looks like to change uint8_t pointer the string itself string
How to make the code to compare these in a if test.

Re: compare unity8_t to a string not working

Posted: Wed Nov 30, 2016 11:46 am
by topguy

Re: compare unity8_t to a string not working

Posted: Wed Nov 30, 2016 12:22 pm
by Canedje
Thanks again.
This was what I needed.