Sign in to follow this  
Tertsi

Any standard way/function like appendtoend(255, 21) = 25521

Recommended Posts

int x = 255 int y = 22 int z = 300 I'd like to be able to simply append them to variable xyz so that with a function call xyz would become 25522300. I know of a bad way to make a function like this but I'm wondering if anyone has an efficient way or knows of a standard C++ function that does this?

Share this post


Link to post
Share on other sites
If you know how many digits they're supposed to be then just multiply by 10^x, if you don't, then what's the point of this? You can't get your numbers back. Perhaps you would rather use strings instead of integers.

Share this post


Link to post
Share on other sites
Nope. Basically, just do the arithmetic:


int digitConcatenate(int prefix, int suffix, int base = 10) {
int counter = suffix;
int shifted = prefix;
do { shifted *= base; counter /= base; } while counter;
return shifted + suffix;
}

Share this post


Link to post
Share on other sites
I don't need to be able to get the numbers back from that xyz (or whatever) number.

It's not pre-determined how many digits they are supposed to be. I could of course do this with if statements so that I wouldn't need to know how many digits they are supposed to be but that is the 'bad way' that I mentioned.

Share this post


Link to post
Share on other sites
Well, here's a function:


#include <cmath>

int appendTo(int a, int b)
{
return b + a * static_cast<int>(std::pow(10.0, 1.0+std::floor(std::log10(static_cast<double>(b)))));
}




It doesn't do anything interesting with negative numbers, but then again I'm not really sure what appending -5 and -4 would be supposed to yeild.

Why you would need such a function beyond me, so back to my question, what's the point of this?

Share this post


Link to post
Share on other sites
The safe C++ way is to use:


#include <sstream>
std::stringstream ss;
ss << x << y << z;
int result;
ss >> result;
return( result );

// Or you could directly return a string:

return ss.str( );





An unsafe C-only alternative is:


buffer[256];
sprintf( buffer, "%i%i%i", x, y, z ); //Unsafe, can overflow!
return( atoi( buffer ) );



Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
sprintf is a safety hazard, use snprintf instead.
On the other hand snprintf isn't standard and would silently cut of the string if additional buffer space were required.
A better solution would be to approximate the maximum required buffer size instead.
enum { MAX_DIGITS = sizeof(unsigned) * CHAR_BIT / 3 };
char buf[MAX_DIGITS * 2 + 1];
However the arithmetic solution is probably preferable..

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
On the other hand snprintf isn't standard


[begin nit-pick]

Actually it is but its part of the C99 standard and C++ 0x03 TR1 standard ( std::tr1::snprintf ).

[end nit-pick]

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Actually it is but its part of the C99 standard and C++ 0x03 TR1 standard ( std::tr1::snprintf ).
Yeah, I guess you're right.
I should've written "snprintf() isn't portable in practice" instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
If you have a modern up to date C or C++ compiler and it doesn't have snprintf, someone needs to be beaten.
*cough* VC6 *cough*
Might not be all that modern but it's widely used, and most importantly I often use it myself.
Although it's got _snprint but that'd rather avoid platform specific #ifdefs if possible.

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
cough* VC6 *cough*


Quote:
Original post by smart_idiot
If you have a modern up to date C or C++ compiler and it doesn't have snprintf, someone needs to be beaten.


*cough*

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this