How to convert array to a string ?

Hi

This is part a code that receives data and the length of the data..
How do I convert the payload which is an array held in memory ( I think ?), into a string so that I can compare if it is equal to test ?

My attempt is obviously wrong and I have tried to find the answer but couldn't :stuck_out_tongue_closed_eyes:

Thanks

Regards Gary

void callback(char* topic, byte* payload, unsigned int length) 
{
   if (payload == "test")
    {
     Serial.println(payload);
   }      
 }

To compare null terminated C strings, use "strcmp"

How do I convert the payload which is an array held in memory ( I think ?), into a string so that I can compare if it is equal to test ?

You don't need to. The strcmp() function can be used to perform the test, and the payload array can be cast to the proper type.

if(strcmp((char *)payload, "test") == 0)
{
   // Yeah, they match.
}

Hi

For some reason that didn't get a match ?

Is it because payload is a byte*

Regards

Gary

Is it because payload is a byte*

No, the cast takes care of that.
Is payload null terminated?

Hi,

I didn't write the program but I beleive its not as thats what the length is doing ?

heres a link to the page Arduino Client for MQTT · knolleary

If you scroll to Comment 50 and see nick • June 22, 2012 , he is asking a similar question , but he wasn't trying to get a match ?

Thanks

Regards

Gary

You could maybe add a debug line to print whatever payload contains and end the mystery?

I didn't write the program but I beleive its not as thats what the length is doing ?

I don't understand what you're saying - you don't use "length" in the code you posted

Just post your code.

Hi

Yes I did that and I just used "t" and it was printed out correctly anthough it was the ascii representation printed 116

for (int i = 0; i < length; i++)
     {
       Serial.println(payload[i]);  
     }

Thanks

Regards Gary

Hi

This is the full code so far

/*
 Publishing in the callback 
 
  - connects to an MQTT server
  - subscribes to the topic "inTopic"
  - when a message is received, republishes it to "outTopic"
  
  This example shows how to publish messages within the
  callback function. The callback function header needs to
  be declared before the PubSubClient constructor and the 
  actual callback defined afterwards.
  This ensures the client reference in the callback function
  is valid.
  
*/

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte server[] = {192,168,0,2};                                                               // If its a numerical address use this and comment out above line
byte ip[]     = {192,168,0,3};                                                              // Update this with value suitable for this network.        
byte mac[]    = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  

String store;

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// ___________________________________________________________________________________________________________
//                                                                                           Callback function

void callback(char* topic, byte* payload, unsigned int length) 
{
  client.publish("outTopic", payload, length);
        
        Serial.begin(9600);
   
        Serial.println("Arduino got payload ...");
     
   for (int i = 0; i < length; i++)
     {
       Serial.println(payload[i]);  
    
     }   
       
       if(strcmp((char *)payload, "t") == 0)
       {
        Serial.println("Arduino got match ...");
       }
       
}

// ____________________________________________________________________________________________

void setup()
{
  
  Ethernet.begin(mac, ip);
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
  }
}
// ____________________________________________________________________________________________

void loop()
{
  client.loop();
}


Regards  Gary

Try this (the old cut&paste), tested and works.

void setup(void) 
  {
  Serial.begin(9600);
  Serial.println("testing...");

  char text[ 16 ];
  Serial.println( "\n\n" );
  strcpy( text, "text" );
  for (int i = 0; i < strlen( text ); i++)
  {
    Serial.println((char)text[i]);  
  }   
}

void loop(void) {
};

Hi,

Yes that printed

t
e
x
t

Regards Gary

And you see the difference to print ASCII text in the print statement?

The byte array is already a C string if it has a NULL byte at the end. NULL is zero.

Here are the C string library commands Arduino has:
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

The main page that is on has all the standard libraries for AVR GCC:
http://www.nongnu.org/avr-libc/user-manual/modules.html

There are many sites with C/C++ instruction, how-to's and examples on the net but beware that Arduino environment is very small and not suited to some approaches, especially those involving creating and deleting code objects (sometimes you don't know that's what's happening, C++ String objects for example automatically do it with -every- change in string length) and re-entrant code is ram-heavy as well.