converting a 2 char array to string return 5 bytes instead of 2???

Hi,

It’s driving me crazy, I can’t get a solution to this problem.

When executing the whole code (second code part); tmpstr.length = 5; which is incorrect because it must return 2.
When executing only the 4 lines from code part 1 it went well.

i’m busy with this problem for 16 hours i think… I don’t know what to do; can someone help me? :fearful:

		char retstr[2] = {65,65}; // outputnum[i],outputstatus[i]}; 

		tmpstr = retstr;
		Serial.print("LENGTH:");
	Serial.println(tmpstr.length());

here is a part of the code with the error

const int outputcount = 4; //Output array Count 
int outputnum[outputcount] = {9, 10, 11, 12}; //1-13 Digital pins, 14-19 Analog Pins A0-A5
int outputstatus[outputcount]; //the status of the output of de ingang van het relais (zie relayinput)
int lastoutputstatus[outputcount];
unsigned long outputswitchtime[outputcount];

//omdat het relais op de uitgang een puls relais is, wordt er een terugmeldcontact ingelezen;
//bij het opvragen van de outputstatus, wordt de status van deze ingang weergegeven.
const int relayinput[outputcount] = {5, 6, 7, 8};
unsigned long outputofftime[outputcount]; //the time that this output must auto turn off (for relays): no auto off = 0
//</OUTPUT HANDLE MODULE>


#define debug true //Debug mode ON

void setup()
{

	Serial.begin(9600);

	initoutputs(); //INIT SELECTED OUTPUTS

	String outputdata = buildOutputstring(); //ZET ALLE OUTPUTGEGEVENS IN EEN STRING

}


void loop() {

	
}


String buildOutputstring() {
	//Zet alle uitgangen om naar 1 string, welke de master kan verwerken.	
	
	String tmpstr;// = S_INITIO; //Command for IO Request
	//char retstr[2];

	for (int i = 0; i < outputcount; i++) {
		char retstr[2] = {65,65}; // outputnum[i],outputstatus[i]}; 

		tmpstr = retstr;
		Serial.print("LENGTH:");
		Serial.println(tmpstr.length());
	}
	return tmpstr + "|";
}




void initoutputs() { //INIT THE SELECTED DIGITAL PINS AS OUTPUT
    int thhisoutput = 0;	
    for (int i = 0; i < outputcount; i++) {
		thhisoutput = outputnum[i];


		pinMode(thhisoutput, OUTPUT);

		if (debug == true) {
			Serial.print("Loaded OUTPUT");
			Serial.println(thhisoutput);
		}

	}
}

when deleting the line thhisoutput = outputnum*;[/b], the arraysize is correct…*
but i can’t find any error, i hope someone can tell me the reason and give me an solution.
Greets,
Reinald Nijboer,
the Netherlands

char retstr[2] = {65,65}; // outputnum[i],outputstatus[i]};

That is not a two character string. It is an array containing two characters. To be a string is must be terminated with a null character:

char retstr[] = {65,65,'\0'}; // outputnum[i], outputstatus[i]}; null terminator

know its an char array with 2 bytes. in the sample code the definition for tmpstr was missing. Converting can done by this i thought..

        char retstr[2] = {65,65}; // outputnum[i],outputstatus[i]}; 

        String tmpstr = retstr;

Am i right or do i think in the wrong way?

Am i right or do i think in the wrong way?

No and yes.

You are expecting the String class to deal with the array without you telling it how many elements are in the array. As John pointed out, that is wrong.

Ok

so when adding the /0 to the chararray You tell in the conversion what the last element is?

Reinosoft: when adding the /0 to the chararray You tell in the conversion what the last element is?

Yes. A byte containing 0 indicates that the previous character is the last character in the string.

johnwasser: Yes. A byte containing 0 indicates that the previous character is the last character in the string.

Another way to think of it is that the NULL is a STOP sign. It tells any function that expects a string, that is going to read and process the char array, where to stop reading and processing.