Question: I am working in a mqtt application and when i receive the information of the payload from the mqtt broker and try to convert it from the void\* that is message->payload to an int as 

```
signed int var_1=*((int*) message->payload);

```

instead of converting it to the number it is converting it to another one, to see this I am using the following code:

```
printf("Message:%s\n",message->payload);
printf("Message:%i\n",var_1);

```

Which shows:

```
Message:-58
Message:3683629

```

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.

```

Answer: 
> 
> [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*).
I didn't quite understand if message->payload holds the number itself or a string representation for it.
If message->payload holds the **memory location** in which the number is stored, so var\_1 holds the **value**.
Therefore, you cannot expect those values to be the same.
Regarding stoi - it receives a string that hold number, but as a string. For example - 

```
std::string num = "1234";
int convertedNumber = stoi(num);

```