Need help in converting string to uint8_t

Hello,
I have searching for method on how to convert string to uint8_t since few days, none solved my problem, please some one help me with this.
Here is my code,

#include <RHReliableDatagram.h>
#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup() 
{
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
}
String c1 = "";
int num = 1;
char c;

void loop()
{

  if ( Serial.available() )
  {
    char c = Serial.read();
    num = 1;
    if (c == 'e') //'e' represents end of input
    {    

      Serial.println("Sending to nrf24_server");      
      //uint8_t data[] = "Hello World!";
      uint8_t data[] = c1; //Write value of 'c1' to data
      nrf24.send(data, sizeof(data));
      nrf24.waitPacketSent();
      // Now wait for a reply
      uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
      uint8_t len = sizeof(buf);

      if (nrf24.waitAvailableTimeout(500))
      { 
        // Should be a reply message for us now   
        if (nrf24.recv(buf, &len))
        {
          Serial.print("got reply: ");
          Serial.println((char*)buf);
        }
        else
        {
          Serial.println("recv failed");
        }
      }
      else
      {
        Serial.println("No reply, is nrf24_server running?");
      }        
      Serial.println(c1);
      c1 = "";
      c = NULL;
      num = 0;
      //delay (500);

    }
    if (num == 1)
    {
      c1.concat(c);
    }   
  }
}

At line 39, I just wrote uint8_t data[] = c1;, please help me with converting string type to uint8_t variable.

The first thing to say is that you do not have a string to convert to an integer, you have a String.
If you insist on using a String rather than a string then the String toInt() function would seem to be what you want.

See http://arduino.cc/en/Reference/StringToInt

UKHeliBob:
The first thing to say is that you do not have a string to convert to an integer, you have a String.
If you insist on using a String rather than a string then the String toInt() function would seem to be what you want.

See toInt() - Arduino Reference

Thank you for your time,

Is it possible to convert alphabets from 'String' to 'uint8_t' using 'toInt()' ? I want to send messages like 'hello, how are u?'.

EDIT: The description of toInt() says, "Converts a valid String to an integer. The input string should start with an integer number. If the string contains non-integer numbers, the function will stop performing the conversion."
As my Sting contains non integer values, may be it won't work for me. Is there any method for non integers?

Is it possible to convert alphabets from 'String' to 'uint8_t' using 'toInt()' ? I want to send messages like 'hello, how are u?'.

Each letter of the alphabet has a unique and widely understood numeric value associated with it. Do some research on ASCII to get the details, but I don't really understand your question. The answer is no, by the way. What is it that you want to do exactly that needs a String turned into a series if ints representing the letter of the String ?

Finally my problem solved... :slight_smile:

Here is my modified code:

#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24;
void setup() 
{
  Serial.begin(9600);
  while (!Serial)     ; 
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
}
String serialRead = "";
int num = 1;
char c;

void loop()
{
  if ( Serial.available() )
  {
    char c = Serial.read();
    num = 1;
    if (c == 'e') //'e' represents end of input
    {    
      Serial.println("Sending to nrf24_server");   
   
      /////The following lines are added/modified.-----------------------------------------------
      char data[10];
      serialRead.toCharArray(data, 10);  // Converted String to char.
      nrf24.send((uint8_t *)data, sizeof(data)); // Converted char to uint8_t (correct me if I was wrong)
      ////Till here.---------------------------------------------------------------------------------

      nrf24.waitPacketSent();     
      uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
      uint8_t len = sizeof(buf);
      if (nrf24.waitAvailableTimeout(500))
      { 
        // Should be a reply message for us now   
        if (nrf24.recv(buf, &len))
        {
          Serial.print("got reply: ");
          Serial.println((char*)buf);
        }
        else
        {
          Serial.println("recv failed");
        }
      }
      else
      {
        Serial.println("No reply, is nrf24_server running?");
      }        
      Serial.println(c1);
      c1 = "";
      c = NULL;
      num = 0;      
    }
    if (num == 1)
    {
      c1.concat(c);
    }   
  }
}

Does that code compile ?

Yup, I got the correct output at the other arduino using nrf24 module.
EDIT: Forgot to declare String serialRead = "";. Now it is compiling.

BTW, I am not using the above code with my aduino, I have used this code to explain my problem that I have faced in different sketch (As that sketch is very lengthy, I have used this code to post my issue).

EDIT: Forgot to declare String serialRead = "";. Now it is compiling.

That would improve it a lot
I must say though that I don't approve of the name you gave the String as it could get very confusing.

nrf24.send((uint8_t *)data, sizeof(data)); // Converted char to uint8_t (correct me if I was wrong)

You are wrong. I think what you have done is to create a uint8_t value that is a pointer to the array that holds the chars but it looks a bit whacky.

UKHeliBob:
You are wrong. I think what you have done is to create a uint8_t value that is a pointer to the array that holds the chars but it looks a bit whacky.

Is there any alternate method to get the result which matches the result of above code? I have been trying since 3 days to send string (message) over nrf24 module using radiohead library, but I am not able to do it until the line that you have quoted is used in my sketch. If you want, I can post the code where I have used the above line. Now my sketch is working as I need.

I didn't say that what you have done wouldn't work. If it is working that is fine.

What the send method requires is a pointer to a C style string (an array of chars terminated by a zero) You could do what you want by using strings instead of Strings throughout the program.

Declare a pointer to an array of chars and an integer or byte variable to be used as the index to the array. As each character is received save it to the array in the current position and increment the index variable. When the terminating character is received put a zero in the array at the current position. The array will contain the data to be sent and the index variable will hold its length which can both be used in the send method.

UKHeliBob:
I didn't say that what you have done wouldn't work. If it is working that is fine.

What the send method requires is a pointer to a C style string (an array of chars terminated by a zero) You could do what you want by using strings instead of Strings throughout the program.

Declare a pointer to an array of chars and an integer or byte variable to be used as the index to the array. As each character is received save it to the array in the current position and increment the index variable. When the terminating character is received put a zero in the array at the current position. The array will contain the data to be sent and the index variable will hold its length which can both be used in the send method.

Your suggestion looks quite interesting. Can you please give me an example? I hope, that can help me optimise my code.

See http://gammon.com.au/serial