# How to join two chars together

i new to aurdino and i have a question. lets say the char a=3 and char b=4 and car c=7. how would i join them together and turn them into like int abc=347.

Have you thought about trying something like this?

``````int abc = 100*a + 10*b + c;
``````

I suspect that the OP is looking for something such as the strcat(...) function, but first the OP would need to learn about arrays and about c-strings. I wonder if the OP is really trying to do something, what that something is, and whether this is an x-y problem.

In any case, while the suggested solution works for the parameters given by the OP, this could not work for an ASCII char that could have a value from zero to 127. Multiplying by 10 or 100 is insufficient.

if the characters are ASCII subtract '0' to get the integer, e.g.

``````  char a='1';
int ia=a-'0';
printf("%d %d",a, ia);
``````

gives

``````49 1
``````

in practice one should check the the character does contain a digit before converting

``````char aStr[10];
int val;

aStr[0] = a;
aStr[1] = b;
aStr[2] = c;
aStr[3] ='\0';
val = atoi(aStr,);
``````

-jim lee

you can use the function result of sscanf() to check the conversion was sucessful, e.g.

``````  char abc[]="123";
if(sscanf(abc,"%d",&ia)==1)printf("integer %d\n",ia);
else printf("conversion failed\n");
``````

gives

``````1integer 123
``````

a function result of 1 indicates one sucessful conversion

master code:

``````void setup()
{

Serial.begin(38400);
}
void loop();

Serial.print("111");
Serial.print("222");
Serial.print("333");
Serial.print("444");
Serial.print("555");
Serial.print("666");
Serial.print("777");
Serial.print("888");
Serial.print("999");
Serial.print("000");
``````

slave code:

``````void setup()
{

Serial.begin(38400);

}
void loop()
{

if(Serial.available()>=0)
{

Serial.println(num1);
Serial.println(num2);
Serial.println(num3);
Serial.println(num4);
Serial.println(num5);
Serial.println(num6);
Serial.println(num7);
Serial.println(num8);
Serial.println(num9);
Serial.println(num10);
}

}
``````

when i run the slave code i get something like this in the serial moniter
???
???
???
4
445
556
111
222
333
888
999
000
444
555
666
777
111
222
333
444

i want it to stay like

111
222
333
444
555
666
777
888
999
000
111
222
333
444
555

can someone help me i posted a fourm in the netwroks and protocols section they said that there was nothing wrong with the bluetooth i even looked at the serial inputs and used there code i get the same result.

That code is nonsense. Not even close to viable.

``````char num1[4]={Serial.read, Serial.read, Serial.read, '\0;}
``````

What ?

`if(Serial.begin()>=0)` not sure - does begin even return anything?

...
Serial.print("111");
Serial.print("222");
...

Your output for just these two would be "111222".

Now, how do you know one number has ended and the other has begun?

-jim lee

A read of Serial Input Basics is in order.

sorry my bad i had the code in another computer and i was in a hurry, so i had to write it manually, sorry ill fix it. i just typed it down quickly without really paying attention to the code. again i apologize for error i am deeply sorry, and thank you.
the emojis are question marks idk why it turned them into emojis.
is there a solution to my problem?

the emojis are question marks idk why it turned them into emojis.

It's because you didn't use code tags around your code so some of it has been interpreted as HTML commands

the problem is that num1 should be equal to 111 and num2 should be equal to 222 same on for all the other num but the number keeps changing

daudhtm:
the problem is that num1 should be equal to 111 and num2 should be equal to 222 same on for all the other num but the number keeps changing