[I] try to convert it from the void*
... to an int
.
OK. The first step is to figure out the type of object that is being transported. As the documentation of MQTT says:
MQTT is data-agnostic and it totally depends on the use case how the payload is structured. It’s completely up to the sender if it wants to send ...
as
signed int var_1=*((int*) message->payload);
Now, this is correct, if the pointer points to an object of type int
. It is a reasonable guess, but you should not be guessing - except as last resort - for the type of the object. You should study the sender whether by reading documentation or code to find out the type of the pointed object.
instead of converting it to the number it is converting it to another one
So, either you've been expecting the wrong value, or you guessed the type wrong. The solution is to stop guessing and find out the correct type.
I also thought about the payload being a string, but if I use the stoi function it gives me the error:
can not convert from ´void*´ to ´int´ with stoi function.
The error seems to be exceptionally clear. The argument of stoi
is const std::string& str
, not void*
. void*
is not implicitly convertible to std::string
. Exactly how to do such conversion depends on what type of object void*
points to (or sometimes, what type of data it contains).