Concat values to string?

Hi there, i am quite new to strings in Arduino, read the man page but cannot figure out why this does not create a string like so: "12:13:41:32"

  `String currTime = String(hours < 10 && '0' + hours + ':' + minutes < 10 && '0' + minutes + ':' + seconds < 10 && '0' + seconds);`

When i Serial.println(currTime) i just get a huge int number back...

Cheers and Thanks!

What does

`String currTime = String(hours < 10 && '0' + hours + ':' + minutes < 10 && '0' + minutes + ':' + seconds < 10 && '0' + seconds);`

this hours < 10 and this minutes < 10 and this seconds < 10 supposed to do?


String currTime =""
if ( hours < 10 )
currTime = "0" + String(currTime);
else {
currTime= string(hours);
}
currTime = currTime + ":";
///do the same with minutes and seconds.

might be or might not be a clue for you.

You code shows a lack knowledge of these things,

and

and

Thanks - but please look at that:

int main()
{
    int value = 1;
    //ternary
    int check = value < 2 ? 1 : 0;
	  printf("%i", check);
//check inline
int check2 = value < 2  && 1;
	  printf("%i", check2);
    return 0;
}

made this available here:

my version + ternary operators should totally work... i guess...^^

Cheers

Good luck.

Please tell me if i am wrong - i am just starting with arduinos.
Is there a reason not to use this code above?
i mean your code does do the trick too - but with way more writing..

You are wrong if you think that your code in post #1 has something similar with example code in post #3...

Is the fact that your code doesn't work not enough reason not to use it?

consider

 the time is 11:59:59

char s [80];

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    byte hr  = 11;
    byte min = 59;
    byte sec = 59;

    sprintf (s, " the time is %2d:%02d:%02d", hr, min, sec);
    Serial.println (s);
}

// -----------------------------------------------------------------------------
void loop ()
{
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.