Is there a way?

Started by
4 comments, last by dmatter 13 years, 2 months ago
Hey, I was just wondering if there was any possible way to do a certain task. Say you have two integers 4 and 16.
I was just wondering if there was any command that puts these two integers together so you have a new integer of 416.
I cant think of any way to do this besides maybe putting it into a string and what not however I dont want to use a string, I need
it to be in integer form. Thank you!
l jsym l
Advertisement
If you know how many digits the second number is, just multiply the first number by 10 raised to the power of that then add. You can find the number of digits by taking the log base 10.
Well, what you're talking about is a string concatenation operation, so using strings is one of the most obvious solutions...

Another way would be to count how many columns the 2nd number occupies when converted to base 10. For "16" the result of that calculation would be 2. You'd then multiply the first number by 10^result (e.g. 10^2 or 100) and add it to the second number.
e.g. 4*100 + 16 = 416

[edit] ninjad
alright thanks i'll try that out.
l jsym l

If you know how many digits the second number is, just multiply the first number by 10 raised to the power of that then add. You can find the number of digits by taking the log base 10.


floor(log(n)) + 1 to be exact :)
Humble people don't refer to themselves as humble (W.R.T. "IMHO".)
Something like:

int concat(int first, int second)
{
std::stringstream ss;
ss << first << second;
int result;
ss >> result;
return result;
}


Although you'll want to decide what to do about negative numbers; perhaps by using unsigned integers to disallow them completely.

This topic is closed to new replies.

Advertisement