-1

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.
  • what are you asking here exactly? what's `message->payload` supposed to be anyway? a buffer representing `int` or ascii string? – David Haim Oct 10 '17 at 08:27
  • 1
    The real question is, how is the message sent? The sender did something to create a payload from his data. You need to reverse this process. – Sebastian Redl Oct 10 '17 at 08:28
  • `instead of converting it to the number it is converting it to another one` What is the number that you expected then? – eerorika Oct 10 '17 at 08:33

3 Answers3

1

In C++ you can't automatically convert from const void* to const char*.

You need an explicit static cast:

int i=atoi(static_cast<const char*>(message->payload));

Notice I've used atoi() which is a C library function (#include <cstdlib>).

There's little point converting it to a std::string just to parse that to an int.

All this assumes you're correct to think the payload is a character encoded decimal integer in a c-style string.

Persixty
  • 8,165
  • 2
  • 13
  • 35
1

[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).

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
0

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);
Roy Rashti
  • 196
  • 2
  • 15