Need help for adding ints in a char

Hello, I need in my code a character combined from ints. More specifically:
I need this:

char PIN[5] = "1234";

I have these:

int a = 1;
int b = 2;
int c = 3;
int d = 4;

How can I merge the 4 ints into one char? I tried concat without any luck

byte a = 1;
PIN[0] = a;

Thank's for your reply.

There's being a problem though.

When I test this:

byte a = 1;
byte b = 2;
byte c = 3;
byte d = 4;

char PIN[5] = "";

PIN[0] = a;
PIN[1] = b;
PIN[2] = c;
PIN[3] = d;

Serial.print("pin:   ");
Serial.print(PIN);
        

I get this at Serial monitor:

pin:   

It doesnt show char's content

yes. Serial print does not know what you mean and decide to show a character with ASCII numbers 1 2 ...
to force print showing value as decimal number:

Serial.print(PIN[0], DEC);
Serial.print(PIN[1], DEC);
...

if it not to store but to be printed out, then you should convert digits to refered character

  Serial.begin(115200);
  byte a = 1;
  byte b = 2;
  byte c = 3;
  byte d = 4;

  char PIN[4];

  PIN[0] = a + 48;
  PIN[1] = b + 48;
  PIN[2] = c + 48;
  PIN[3] = d + 48;

  Serial.print("pin:   ");
  Serial.print(PIN);
1 Like

Well, your suggestion works fine but it doesn't respond well with a library that I use, so I found out this way that works. I'll post it for anyone looking something like this.

int a = 1;
int b = 9;
int c = 9;
int d = 7;

char PIN[5];
        
snprintf(PIN, 5, "%d%d%d%d",  a, b, c, d);

Serial.print("pin:   ");
Serial.print(PIN);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.