Convert char Array to String

Hi,

I am reading data from an RFID reader using software serial. The data is being received Byte by Byte and placed into a char array. The sketch below works great and displays the data perfectly, but I would like to find a way in which I can convert the char rfidData[Buffsize] into a string. I want a string so that I can call this string variable elsewhere in my code and it can be processed. The other part of the code is sending data with a GSM shield, so it must need the data in a string format. Does this make sense?
The whole thing has me a little confused.

Any help would be great.

#include <SoftwareSerial.h> 
SoftwareSerial RFID(A8, A9); //pin2 Rx, pin3 Tx 
#define BUFSIZE    15  // Size of receive buffer (in bytes) (10-byte unique ID + null character)
const int rfidon =  A5;


void setup() 
{ 
pinMode(rfidon, OUTPUT);
Serial.begin(9600);
RFID.begin(9600); // set the data rate for the SoftwareSerial port 

digitalWrite(rfidon, HIGH); 
}



void loop() 
{

char rfidData[BUFSIZE];  // Buffer for incoming data
int G = Read_RFID_Tag(rfidData);   //**
if (G == 0){
  Serial.println(rfidData);
}

}




int Read_RFID_Tag(char *const rfidData)
{
  long setime = millis();
      char offset = 0;
      byte C =0;
        RFID.write(0x53);//This command sets the reader active (Command SRA)
        RFID.write(0x52);//**
        RFID.write(0x41);//**
        RFID.write(0x0d);//**
      while(millis() - setime < 2000){
            if (C == 13 && offset == 17){
              return 0;
              //break;
            }
            rfidData[BUFSIZE];  // Buffer for incoming data
            rfidData[0] = 0;         // Clear the buffer
            offset = 0;         // Offset into buffer      

            while (RFID.available()) { 
                C = RFID.read();
                rfidData[offset] = C;
                offset++;
                delay(1);
                //Serial.print(C);
                if(C == 13 && offset == 17){
                    //rSerial.println("");
                    //Serial.println(rfidData);
                    return 0;
                    //break;
                  }
                }
      }
        RFID.write(0x53);//This command sets the reader deactive (Command SRD)
        RFID.write(0x52);//**
        RFID.write(0x44);//**
        RFID.write(0x0d);//**
}

The difference between a character array and a string (small s*) is the null terminating character (’\0’). An array has no null terminator and a string does. Make your receive buffer, at least, large enough to hold the character array plus 1 extra element for the null. After you read the last character append a null to the array and you have a string.

*string-null terminated character array as opposed to a String-an object of the String class.

Thanks for the prompt input. I appreciate the advise, and it makes sense. If I added the following line after I have filled my char array:

rfidData[17] = '\0';
string myNewString = rfidData;

A I declaring this correctly?

Also, why the small "s"? The arduino IDE does not seem to recognize the string with a small s...?

A I declaring this correctly?

If you declare a 15 element array, and write into position 17 (of 14), then, no you are not.

The NULL goes after the last character added, EVERY time you add a character. It does NOT go after the end of the array.

Yes, I understand. Although, I have assigned a buffer of 15, my offset position seems to 17 after I receive all of the data from the RFID correctly which has me confused. Why would that be? Should I increase the size of the buffer to allow for the null character?

I have made the following changes:

#define BUFSIZE    18

Is this the line of code I need to make the conversion?

String myNewString(rfidData);

Should I increase the size of the buffer to allow for the null character?

You MUST do that.

If you are reading 17 bytes from the RFID reader, you MUST size your array to hold 17 (or more) bytes.

That you haven't been burned yet means that you are trashing memory that is not otherwise being used. It's far better to be good than to be lucky, though.

Is this the line of code I need to make the conversion?
Code: [Select]

String myNewString(rfidData);

You do not need to convert the null terminated character array rfidData to anything else.

I want a string so that I can call this string variable elsewhere in my code and it can be processed. The other part of the code is sending data with a GSM shield, so it must need the data in a string format.

rfidData when null terminated is a "string" (small 's') and can be processed in any way you like using a powerful tool kit of standard string functions.

Commonly used functions are:
strcat - concatenate two strings
strchr - string scanning operation
strcmp - compare two strings
strcpy - copy a string
strlen - get string length
strncat - concatenate one string with part of another
strncmp - compare parts of two strings
strncpy - copy part of a string
strchr - string scanning operation
strtok- divide a string into pieces

You do not need and should avoid "String" (capital 'S') which can lead to memory problems.