Is there an equivalent to substring for char arrays? For instance in the code below the myString.substring(4, 6) gives me gives me a result of 56 but I can’t find a function which does the same for myChar.
Any help would be appreciated, all the searches that I have done come up with String solutions rather than char solutions.
just a note to add that the '0' initialisation of the subString is important otherwise subString would likely not be properly terminated with the null char after the strncpy.
if you want to go a small bit faster esp if your buffer is long (no need to put null characters everywhere in the buffer to override them just later and just really need one at the end
char myChar[] {"123456789"};
byte numberOfBytesOfInterest = 2;
char subString[numberOfBytesOfInterest+1] ;
strlcpy(subString, myChar+4, sizeof subString); // strlcpy will also ensure you don't overrun the buffer
when you define an array you give the number of elements so 6 elements, going from index 0 to index 5
➜ char MAC_Parts[6][3];
the 3 means you can have 2 significant characters, the third one will be the trailing null char added by strlcpy()
for your code note that you could just iterate through the String using String functions to extract the pieces
something like this, typed here so let you test it.
String mac = "EC:FA:BC:9F:21:EB";
void setup() {
Serial.begin(115200);
Serial.print("ESP Now MAC Address {");
int pos = 0, colonPos = 0;
while ((colonPos = mac.indexOf(':', pos)) >= 0) {
if (pos != 0) Serial.print(", ");
Serial.print("0x");
Serial.print(mac.substring(pos, colonPos));
pos = colonPos + 1;
}
if (mac.charAt(pos) != '\0') { // handle the last one which does not have a colon afterwards
if (pos != 0) Serial.print(", ");
Serial.print("0x");
Serial.print(mac.substring(pos));
}
Serial.print("}");
}
void loop() {}
or you could go and print until you find a column or a null char and build the text as you go.
String mac = "EC:FA:BC:9F:21:EB";
void setup() {
Serial.begin(115200);
Serial.print("ESP Now MAC Address {");
int pos = 0;
bool done = false;
while (not done) {
switch (mac.charAt(pos)) {
case '\0': Serial.println("}"); done = true; break;
case ':' : Serial.print(", 0x"); break;
default : if (pos == 0) Serial.print("0x"); Serial.write(mac.charAt(pos)); break;
}
pos++;
}
}
void loop() {}
I keep learning about programming in bits. Is there a definitive reference book on C++? I'm sure there are a lot but is there one which is the bible?
When i got my comadore 64, a million years ago, it came with a book covering all the syntax. I read it front to back, and whilst it didn't all stick, enogh did so that i new where to look when I had a problem.
I'm a bit old fashioned and I prefer paper reference books.
Unfortunately, C++ is a much larger and more complex language than Comadore 64 BASIC. A "definitive" book would be huge and would serve neither beginners nor experienced programmers well.
I understand the appetite for a paper format but would advise to get started first with on line materials if you are a hands on learner
There is a lot available like C++ Introduction to get you started where the web site lets you type and try code directly on line. It helps anchor knowledge and you don’t get distracted by terminal command lines and files on your machine etc… (but there is advertising).
you can get a book for paper reference as well if you are more of a conceptual learner willing to get acquainted with theoretical knowledge without even trying everything - that works for some too esp if you master a few other OO programming languages. I heard good things about The C++ Primer(not c++ primer plus) as a first book. You also have Scott Meyers' Effective C++ books worth looking at. Of course any good Bookshelf would not be complete without the original book by Bjarne Stroustrup.
My experience has been though that the language and libraries are so rich now that searching on line (cppreference.com and https://cplusplus.com/ are good sources) or even asking targeted narrow questions to chatGPT will open new horizons much faster than a 1000 page book.
Investing time in having a good grasp on the syntax, grammar (understanding statements) and the role of types is going a long way. Don’t cut corners there.
Of course if you apply your skills in the arduino world and have questions, the forum is also happy to help out.