Reputation: 11
I have a project to collect a html web page and convert it to a set of strings to be printed. I use arduino IDE to code this application, so java and other solutions do not work as it has to be accepted by arduino IDE. The char array has to be used to collect the incoming characters, but to store the results I need a string. These strings need to be able to take all characters including end of line, carriage return and tab. Html does not use zero characters so, the string can still be terminated correctly.
I have tried to put a zero char at the end of the received string in the char array, but the string still comes out as zero length. I have looked through ardunio.cc and this website, but all the answers are how to convert strings into arrays or are commented that this question has been asked before and NOT answered. This question has not been answered in valid arduino code as even writing a routine that adds individual chars to a string passes the compiler, but does not produce a string when run!
String tempStr = "";
int count2 = 0;
char message[700];
for (int count = 0; count < htmlpage.length(); count++) // maximum string length
{
bufferchr =0;
bufferchr = (htmlpage.charAt(count2+lencount1));
message[count2] = bufferchr;
count2 = count2 +1;
}
tempStr = chararryToStr(message); // fails
tempStr=(message); // also fails
Count does not exist outside the loop, its value is zero. I have included count2
to measure the length of the resulting string in the message array.
All of the string contents come out as zero length, where as printing the char array shows the true results.
This Answers fails to compile on Arduino Uno and gives the following errors which are all down to the arduino core programming not accepting the suggested code. Arduino: 1.8.8 (Windows 8.1), Board: "Arduino/Genuino Uno" ead_string_arduino:69:11: error: no match for 'operator=' (operand types are 'String' and 'String*') tempStr = new String (message); // cast it to string zero length ^ In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:222:0, from C:\Users\stephen\AppData\Local\Temp\arduino_build_685202\sketch\read_string_arduino.ino.cpp:1: C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:86:11: note: candidate: String& String::operator=(const String&) String & operator = (const String &rhs); ^ C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:86:11: note: no known conversion for argument 1 from 'String*' to 'const String&' C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:87:11: note: candidate: String& String::operator=(const char*) String & operator = (const char cstr); ^ C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:87:11: note: no known conversion for argument 1 from 'String' to 'const char*' C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:88:11: note: candidate: String& String::operator=(const __FlashStringHelper*) String & operator = (const __FlashStringHelper str); ^ C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:88:11: note: no known conversion for argument 1 from 'String' to 'const __FlashStringHelper*' exit status 1 no match for 'operator=' (operand types are 'String' and 'String*')
Upvotes: 1
Views: 3748
Reputation: 665
There is no declaration for lencount1 so not exactly sure what your code is doing, but regardless, it's way too complicated.
First, to copy out a C++ String to a C array, there are multiple ways to do this, but strncpy(message, html.c_str(), sizeof (message));
is probably the simplest.
To copy a C++ string into another C++ string, and assuming htmlpage is a String too, then you just need tempStr = htmlpage;
Upvotes: 0