2

I trying to parse some information got from web service.The problem is that I cannot convert a char[] to String. I don't get any errors but the string is empty. Read a lot of pages on internet, including the Convert char array to String, but it does not work for me.

String dataStr = esp8266.readString(); // get the information

const char *dataChar = (const char *)dataStr.c_str();
char * pch;
pch = strstr (dataChar,"nonce"); //finds the first occurrence of nonce
char nonceChar[40];
strncpy (nonceChar, pch, sizeof(nonceChar)); //copy just the part i need
String result (nonceChar); 
Serial.println(result);//it prints nothing
Serial.println(result.length()); //it prints 0

I have also tried with a simple example, but i get the same empty string:

char str2[40]
str2[0]='a';
str2[1]='b';
String v (str2);

Thanks in advance

1
  • 1
    Try null terminating char array Commented Jan 29, 2017 at 22:24

3 Answers 3

2

The problem you illustrate in your example may be due to a null pointer; or you encounter weird behavior because of a non null-terminated char array.

In your small example you construct a char array that may not be null-terminated. I tried it but my compiler (arduino) tends to correct it by accident. Indeed it sees that str2 is like a constant so it puts it in program memory, and nothing in RAM memory. Hence it inializes array with 0. Yet it sees that the 40 cells are not used but the 3 first are simply copied to a String, thus it never creates char str2[40]. Your small code:

char str2[40]
str2[0]='a';
str2[1]='b';
String v (str2);
Serial.println(v);

is usually converted ( / optimized) by the compiler as:

String v ("ab");  // note that "ab" is null-terminated
Serial.println(v);

or:

Serial.println("ab");

Note that that tranformation is an example; the compiler will do other assumptions depending of what is written around.

I dont know exactly what you are doing in your program elsewhere, but I think you can do all with the arduino String class. For instance your main example should be something like that:

  String dataStr("deb,nonce,end");
  int ind = dataStr.indexOf("nonce");
  String result = dataStr.substring(ind);
  Serial.println(result);

I am pretty sure you should not used old c string library; they are complicated to use and very dangerous. For instance strncpy is mostly useless, it gives you nothing more than strcpy. In strncpy(nonceChar, pch, sizeof(nonceChar)); if pch is too long, nonceChar wont be null-terminated ! In fact you could use strlcpy instead (it is available in my arduino linux v1.6.9 compiler).

Also you should check that strstr does not returns NULL pointer. Or better: use indexOf, and check that it does not return -1.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help, your example works, but I noticed the problem comes from the http request that I store in the String "dataStr", for any reason it does not work with the strings i get from the esp8266 buffer and causes the weird behavior.
1

Be sure pch is valid by printing it. Yo do not need to assign your dataStr to an other variable. Last, String class overloading function for equal operator. You can use the sample :

String dataStr = esp8266.readString(); // get the information

char * pch = strstr (dataStr.c_str(), "nonce"); 
char nonceChar[40];
memset(nonceChar, 0, 40);
strncpy (nonceChar, pch, 39);
String result =  nonceChar;

Serial.println(result);//it prints nothing
Serial.println(result.length()); //it prints 0

Comments

0

The problem came from the content of dataStr, the weird behavior was due to anything estrange on the esp8266 buffer. I replaced the String dataStr = esp8266.readString() by String dataStr = esp8266.readStringUntil(Word), being the String variable Word a known word present in the buffer that appears after the string I needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.