How to make a string out of different Boolean variables?

I searched now a lot in the forum, but I just have basic programming knowledge.
So here is my problem:
How can I add different boolean status to a new string variable?

I managed to get what I want in a serial.print output - but now I need just that output as a string.

Here Is what I have done so far to get the right output:

String1 = "U1";
String2 = "5";
Serial.print (String1 + "-" + Boolean1 + Boolean2 + Boolean3 + Boolean4 + "-" + String2);

Above output is looking like this:
U1-0000-5

Or maybe there is anything to get the Boolean 0 or 1 to the string "0" or "1"??

Thanks for your help!

If you are determined to use a String (why ?) then try this

String combinedString = String1 + "-" + Boolean1 + Boolean2 + Boolean3 + Boolean4 + "-" + String2;

DISCLAIMER
I don't use Strings so I am guessing !

If you insist on using the String class

String1 = "U1";
String2 = "5";
String3 = String1;
String3 += "-";
String3 += String(Boolean1);
String3 += String(Boolean2)
…

(Using += helps minimize possible memory issues but this is still costly)

Avoiding use of String class:

char String1[] = "U1";
char String2[] = "5";
char String3[10]; //increase size as required
sprintf(String3, "%s-%d%d%d%d-%s", String1, Boolean1, Boolean2, Boolean3, Boolean4, String2);
Serial.print (String3);

This one is working perfectly - Thank you all for your quick response!

Why I am using this:
I am printing the string to HM10 Bluetooth and the other end is taking that string and extracting the information out of it.

you don't need to build up the string for that, print each element separately

hm10.print (String1);
hm10.write('-');
hm10.write(Boolean1 ? '1' : '0');
hm10.write(Boolean2 ? '1' : '0');
hm10.write(Boolean3 ? '1' : '0');
hm10.write(Boolean4 ? '1' : '0');
hm10.write('-');
hm10.println(String2); // or just print if you don't want the CR LF at the end

You are right, I think - but I rather want to send it in one piece.
But Thanks for your explanation!

But why a String rather than an array of chars built using sprintf() as illustrated in post #4 ?

Which Arduino board are you using ?

I will use sprintf() function !
Board is RP2040 RFM95 LORA

from a serial port point of view, on the sender or receiver, it's totally the same.
you don't send the data in one piece, you send it character by character (byte by byte - or actually bit by bit) and your baud rate is slow compared to executing the various prints, so there will be no pause on the transmission side due to the fact that you had multiple prints.

(and you'll save some RAM if that matters )

I would recommend using snprintf() which checks for array overflow, e.g.

snprintf(String3, 10, "%s-%d%d%d%d-%s", String1, Boolean1, Boolean2, Boolean3, Boolean4, String2);

But only if you use the correct value for the size parameter and it is all too easy to get that wrong or forget to change it to match the buffer being used. This would be better

snprintf(String3, sizeof(String3), "%s-%d%d%d%d-%s", String1, Boolean1, Boolean2, Boolean3, Boolean4, String2);

and check if all worked out OK

  if (snprintf(String3, sizeof String3, "%s-%d%d%d%d-%s", String1, Boolean1, Boolean2, Boolean3, Boolean4, String2) >= sizeof String3) {
    Serial.println("Careful - string got truncated, not enough space");
  }
  Serial.println(String3);

of course String1 and String3 should be c-Strings not the original String instances

String1 = "U1";
String2 = "5";

(or need String1.c_str() and String3.c_str() in the snprintf() call)

I have done this with sprintf function like this code below now, with great success.

  char Status[18];
  sprintf(Status, "U%i-%i%i%i%i%i%i%i%i%i%i%i%i-%d", unitselected, fired_Ch1, fired_Ch2, fired_Ch3, fired_Ch4, fired_Ch5, fired_Ch6, fired_Ch7, fired_Ch8, fired_Ch9, fired_Ch10, fired_Ch11, fired_Ch12, currentPressure,0);
  Serial.println (Status);

Now I ran into another Problem:
The currentPressure variable is a float but I just need the values 1 to 15 coded in the last digit.
So the Last Digit should be like the variable but if it becomes larger than 9.5 it should display "A", >10.5 = B, >11.5 = C, >12.5 = D, >13.5 = E and >14.5 = F

  • like for a Hex count.

I already tried with if commands and with char but with no success!!

could you simply replace %d with %X to print the value as a HEX digit
e.g. if you are dealing with a float variable

void setup() {
  Serial.begin(115200);
  float x=10.3;
  Serial.printf("\n%X", (int) round(x));
  x=10.6;
  Serial.printf("\n%X", (int) round(x));

on a ESP8266 gives

A
B

at the end of the sprintf you have

and you pass

if currentPressure is a float, then you can't use %i


so use %c for a character input and use something like this to get the letter

currentPressure > 14.5 ? 'F' : 
  currentPressure > 13.5 ? 'E' : 
    currentPressure > 12.5 ? 'D' : 
      currentPressure > 11.5 ? 'C' : 
        currentPressure > 10.5 ? 'B' : 'A'

of course you can make it a function

char pressureLetter(float currentPressure)
{
  return currentPressure > 14.5 ? 'F' :
         currentPressure > 13.5 ? 'E' :
         currentPressure > 12.5 ? 'D' :
         currentPressure > 11.5 ? 'C' :
         currentPressure > 10.5 ? 'B' : 'A';
}


void setup() {
  Serial.begin(115200);
  Serial.println(pressureLetter(10.4));
  Serial.println(pressureLetter(10.5));
  Serial.println(pressureLetter(10.6));
  Serial.println(pressureLetter(11.6));
  Serial.println(pressureLetter(12.6));
  Serial.println(pressureLetter(13.6));
  Serial.println(pressureLetter(14.6));
  Serial.println(pressureLetter(15.6));
}

void loop() {}

@All: Thank you so much - this was a perfect lesson and now I solved this part today.

Here is my working code - I didn´t know that it´s so easy to just display a Hex value even in lower and upper Case with %x and %X:

  int cPressure = round(currentPressure);
  char Status[18];
  sprintf(Status, "U%i-%i%i%i%i%i%i%i%i%i%i%i%i-%X", unitselected, fired_Ch1, fired_Ch2, fired_Ch3, fired_Ch4, fired_Ch5, fired_Ch6, fired_Ch7, fired_Ch8, fired_Ch9, fired_Ch10, fired_Ch11, fired_Ch12, cPressure);
  Serial.println (Status);

Output example for currentPressure = 12,2:
U1-000000000000-C

what's currentPressure's value range?

I am fine if it can code until 13, so its more than enough!

OK - I mean if it's above 16 then the %X will lead to 2 digits being generated and you'll overflow your buffer and then all bets are off (that's why snprintf() was a better recommendation)