 # Concat two int

I have been looking to concat two int but with no luck.

My project is reading from a RTC module I have the time in hour & minutes.

The Var below gets the time from the clock lets say the time is 13:42
int NewTimeHr = now.hour();
int NewTimeMin = now.minute();

I try to concat the two numbers together by doing the following (this found on another post)

int NowTime = NewTimeHr*10+NewTimeMin;

If I do a Serial.print(NowTime); You would think the answer is 1342

But no I get 172. I don't know why it's adding only the first digit of the second variable to my first var. e.g. 13 + 4 = 17 How do I get the two int as one number without adding them together?

13*10+42 = 130+42 = 172

That answers what it is doing.

Now to make it do what you want. try

13*100 + 42 = 1300+42 =1342

Enjoy.

Ok but what if the time is 8:30 in the morning 830? would it still be the same?

Seems as if there should be an easier way to concat numbers.

webmaven:
Ok but what if the time is 8:30 in the morning 830? would it still be the same?

Did you try the math?

webmaven:
Seems as if there should be an easier way to concat numbers.

And, not that I know of.

Ok; it worked THANK YOU. I had to reset the clock to test. The post that I used called for val*10+val.

Is 10 wrong? I just want to understand the difference.

What you're doing there is not really concatenating - that refers to combining strings.

What you're doing is just plain old math - addition and multiplication - to result in a number with the hours in the thousands and hundreds place, and the minutes in the tens and ones place. Which when printed looks the same as if you'd concatenated the hours and minutes.

If the second value was only one digit, you would probably want to multiply the first value by 10 instead.

Try:

``````char time;
sprintf(time, "%02d%02d", now.hour(), now.minute());
Serial.println(time);
``````

DrAzzy:
What you're doing there is not really concatenating - that refers to combining strings.

What you're doing is just plain old math - addition and multiplication - to result in a number with the hours in the thousands and hundreds place, and the minutes in the tens and ones place. Which when printed looks the same as if you'd concatenated the hours and minutes.

If the second value was only one digit, you would probably want to multiply the first value by 10 instead.

The assumption is the output should 'resemble' 24 hour clock military time, but the hour's leading zero would be lost.

PaulRB:
Try:

``````char time;
``````

sprintf(time, "%02d%02d", now.hour(), now.minute());
Serial.println(time);

A nice alternative, but the output is not a number data type.