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

This topic is 4693 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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 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 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 on other sites
sprintf is a safety hazard, use snprintf instead.

##### Share on other sites
Quote:
 Original post by smart_idiotsprintf 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 on other sites
Quote:
 Original post by doynaxOn 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 on other sites
Quote:
 Original post by snk_kidActually 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 on other sites
If you have a modern up to date C or C++ compiler and it doesn't have snprintf, someone needs to be beaten.

##### Share on other sites
Quote:
 Original post by smart_idiotIf 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 on other sites
Quote:
 Original post by doynaxcough* VC6 *cough*

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

*cough*

##### Share on other sites

This topic is 4693 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628726
• Total Posts
2984411

• 25
• 11
• 10
• 16
• 14