Coding Help with String + Char (HEX) ...

(deleted)

The serial monitor doesn't do a good job of interpreting non-printable characters. The code you have most likely works.

Edit- until your sketch gets too large or runs for too long, and the String class crashes your program due to heap fragmentation. :slight_smile:

Just keep in mind that having NULL as the first character precludes your string from most C char processing functions.

(deleted)

(deleted)

I think you're right.
If i was you, i'd put my project aside for a couple if days, and work through tutorials on char arrays and processing those 'strings' without the overhead of supporting your specific project.
When you have a modest grasp of chars, pointers and the supporting functions, start looking at your own project again with refreshed eyes!

That code gives me this when serial printed:

mcc.aapp.samsung

Is doing exactly as expected, if you want to play around with String class, then your options are limited.

Why do you need NULL,CR,NULL,mcc.aapp.samsung anyway?

Do you need to build that String or just print it?

Serial.print("\x00\x13\x00");
Serial.print("mcc.aapp.samsung");

will do the same.

indev2:
Is doing exactly as expected, if you want to play around with String class, then your options are limited.

Why do you need NULL,CR,NULL,mcc.aapp.samsung anyway?

Do you need to build that String or just print it?

Serial.print("\x00\x13\x00");

Serial.print(“mcc.aapp.samsung”);




will do the same.

But it still won’t show much in the serial monitor.
I have this function I use when diagnosing strings…

void expandString(char* ptr) {  // utility string function
//#define SHOW_HEX
  char text[8];
  Serial.print(F("\nEXPAND:"));
  while (*ptr) {
    switch (*ptr) {
      case '\r':  Serial.print(F("<CR>")); break;
      case '\n':  Serial.print(F("<LF>\n")); break;
      default:
#ifdef SHOW_HEX
        sprintf(text, "<%2X>", *ptr); Serial.write(text);
#else
        Serial.write(*ptr);
#endif
    }
    *ptr++;
  }
}

You can un-comment SHOW_HEX to see the chars as hex, or add more ‘case’ blocks to display other control characters.

Call it like - so…

expandString("Hello\r\n");

(deleted)

ok, assuming that null is terminating your string as expected, how about building your message in two parts...
The header - NUL strlen(MESSAGE) NULL, followed by MESSAGE itself..

Assuming that you can't .print() this anywhere, because of the leading nulls, but there's nothing to stop you putting that complete message into a buffer and doing anything else with it. Sending bia UDP or whatever.