Trying to find a solution for converting byte* to char*

Hi,

I am new to arduino and I am not a C/C++ programer.

I am subscribing to a MQTT Topic and would like to print the information to an LCD screen. The information from the MQTT Topic is of type byte* and to print the information to the LCD requires type char*. I have been unsuccessful in converting the byte* to char*.

The function to receive the MQTT information is as follows:

void callback(char* topic, byte* payload, unsigned int length)
{
  digitalWrite(13, HIGH);
  Console.println("Callback");
  Console.print("\tTopic: ");
  Console.println(topic);
  Console.print("\tLength: ");
  Console.println(length);
  Console.print("\tPayload: ");
  Console.write(payload,length);
  Console.println();
  digitalWrite(13, LOW);
}

I have tried the following, but it produces an error:

incompatible types in assignment of ‘char*’ to ‘char [7]’

 char LCD_Pres[7];
if (String(topic) == "aujen/weather/current/pressure")
  {
    LCD_Pres=(char *)payload;
   }

I have also tried:

char LCD_Pres[7];
for (int c = 0;c <=length;c++)
    {
      LCD_Pres[c] = char(payload[c]);
      Console.print(LCD_Pres[c]);
    }

This sort of works, the first iteration is correct, but there after it adds a "9" value at the end:

860.28
860.339
860.359

I then reduced the length of LCD_Pres to 6 - char LCD_Pres[6];
This stalls the script and the board hangs, have to power reset board:

Callback
Topic: aujen/weather/current/time
Length: 10
Payload: 1395480336

Callback
Topic: aujen/weather/current/pressure
Length: 6
Payload: 860.36
860.36

Then I came across the sprintf command, but I can't seem to get that to work either:

if (String(topic) == "aujen/weather/current/pressure")
  {
    sprintf(LCD_Pres,"%u6",payload);
Console.print("Pressure: "); Console.println(LCD_Pres);

This prints, I don't know what...

Pressure: 9166
Pressure: 9166
Pressure: 9166

If I try :

sprintf(LCD_Pres,"%u",payload);

or

sprintf(LCD_Pres,"%d",payload);

Both print:

Pressure: 916
Pressure: 916
Pressure: 916

If someone can please point me in the right direction ?

Best I can come up with:

payload[length] = '\0';
String strPayload = String((char*)payload);
strPayload.toCharArray(LCD_Press, 7);

First convert to a String, then to a char* - I would think this is not the most efficient method ?

Which library are you using ?
This one ?

I do not fully understand the paramaters of the callback function.
char* topic : I think this points to a zero terminated string, but I'm not sure, it is not written in the documenation.
byte* payload : This is an array of bytes, but if those bytes are numbers or a string is not in the documentation.
unsigned int length : This is the size of the bytes of 'payload'.

I assume this works: Console.println(topic);
I assume this also works: Console.println(length);

But this one not : Console.write(payload,length);
You can use the payload as a string and cast it to a character pointer: Console.write( (char *) payload, length);

If the incoming byte array is already null-terminated, you can just cast it from (byte*) to (char *).

If it isn't null-terminated, you will need to copy it to a char array with one extra element and append a null terminator. You can do that by a call to memcpy or strncpy.

void callback(char* topic, byte* payload, unsigned int length)
{
    char payloadString[length+1];
    memcpy(payloadString, payload, length);
    payloadString[length] = '\0';

    ... do whatever you want with payloadString
}