Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

compare unity8_t to a string not working

Tue Nov 29, 2016 7:20 pm

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?

User avatar
PeterO
Posts: 5164
Joined: Sun Jul 22, 2012 4:14 pm

Re: compare unity8_t to a string not working

Tue Nov 29, 2016 7:59 pm

Do you understand what a pointer is ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: compare unity8_t to a string not working

Tue Nov 29, 2016 8:04 pm

yes I do.
In the past I did programming. That is a while ago.

User avatar
PeterO
Posts: 5164
Joined: Sun Jul 22, 2012 4:14 pm

Re: compare unity8_t to a string not working

Tue Nov 29, 2016 8:34 pm

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: compare unity8_t to a string not working

Tue Nov 29, 2016 9:08 pm

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.


Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: compare unity8_t to a string not working

Wed Nov 30, 2016 12:22 pm

Thanks again.
This was what I needed.

Return to “C/C++”