# concatenate integers

Hey guys.

I have been looking for a solution to concatenate integers but haven't found anything that works.

I have 2 integers. 1st int is a constant-- int x = 49. 2nd int is a number that varies-- int y = random(0,1024). I want to save this to a 3rd location-- int z= x,y. Example at both extremes of y. z = 490 or 491024

``````int x=49;
int y=0;
int z=0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
y= random(0,1024);

//need concatenate code here

Serial.print ("Y is   ");
Serial.println (y);
Serial.print ("Z is   ");
Serial.println (z);// should be 49 followed by a random number between 0-1024

delay(2000);
}
``````

Hope someone can help. Thanks

``````int x = 49;
int y = 0;
//int z=0;
long z = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
y = random(0, 1024);

if (y < 10)
z = (x * 10) + y;
else if (y < 100)
z = (x * 100) + y;
else if (y < 1000)
z = (x * 1000L) + y;
else z = (x * 10000L) + y;

Serial.print ("Y is   ");
Serial.println (y);
Serial.print ("Z is   ");
Serial.println (z);// should be 49 followed by a random number between 0-1024

delay(2000);
}
``````
``````int x=49;
int y=0;
long int z=0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
y= random(0,1024);
z = x;

do {
z = z * 10;
} while (z < 100L * y);
z += y;

Serial.print ("Y is   ");
Serial.println (y);
Serial.print ("Z is   ");
Serial.println (z);// should be 49 followed by a random number between 0-1024

delay(2000);
}
``````

But do note the version of Paul fails if you change x to something with more digits. If it’s indeed fixed to be 49 you’re good to go.

Otherwise:

``````int x=49;
int y=0;
unsigned int z=0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
y= random(0,1024);
z = 1;

do {
z *= 10;
} while (z <= y);
z *= x;
z += y;

Serial.print ("Y is   ");
Serial.println (y);
Serial.print ("Z is   ");
Serial.println (z);// should be 49 followed by a random number between 0-1024

delay(2000);
}
``````

ROVguy: Example at both extremes of y. z = 490 or 491024

The upper end of the "int" range is 32767. What about using "long" if you want numbers up to 491024?

Or just unsigned... Because all the code provided goes wrong when x is negative so yeay :p

septillion:
But do note the version of Paul fails if you change x to something with more digits. If it’s indeed fixed to be 49 you’re good to go.

No, now I think about it, my code does not work quite correctly even for x=49. Use septilliion’s code!

PaulRB: No, now I think about it, my code does not work quite correctly even for x=49. Use septilliion's code!

Here is another code:

``````void setup() {
Serial.begin(9600);
}

void loop() {
const int x=49;
int y= random(0,1024);
char buf[11];
snprintf(buf,sizeof(buf),"%d%d",x,y); // format 2 numbers as string
long z= atol(buf); // revert string to a long number
Serial.print ("Y= ");Serial.print(y);
Serial.print ('\t');
Serial.print ("Z= ");
Serial.println (z);// should be 49 followed by a random number between 0-1024
delay(2000);
}
``````

BTW: The random number range of "random(0,1024);" is from 0 to 1023. 1024 is not included within that range.

"Sketch uses 4,574 bytes (14%) of program storage space."

vs

"Sketch uses 2,992 bytes (9%) of program storage space."

I think it's a bit overkill to use string functions like snprintf is a bit overkill here...

And yeay, the random() function is a bit weird... I find it not very logical the exclude the upper limit but okay, easy to fix :p

you can calculate the number directly without a loop:

in theory the solution is z = y + 49 * pow(10, 1 + floor(log10(y)));

in practice however you need one additional step…

``````long x = 49;
long y = 0;
long z = 0;

void setup()
{
Serial.begin(115200);

for (int y = 1; y < 1024; y++)
{
z = y + (49 * pow(10, 1 + floor(log10(y))));

Serial.print ("Z is   ");
Serial.println (z);
}

for (int y = 1; y < 1024; y++)
{
z = y + round(49 * pow(10, 1 + floor(log10(y))));

Serial.print ("Z is   ");
Serial.println (z);
}

}

void loop()
{
}
``````

that said,

1. in the integer domain a while loop is often more accurate and faster than the pow & log solution.
2. the pow & log solution does not work for 0 and negative integers.
3. the pow & log solution might increase the footprint.