how to cast int from int[] to const char*

like the topic mention....

int[10] msg;

msg[0] = 1;
msg[1] = 2;

const char* a = (const char*)msg[0];      
const char* b = (const char*)msg[1];

It seem there is no value when I test by printf

It seem there is no value when I test by printf

There is no value, or the value is zero?

What are you trying to do?
Do you know what data is at addresses 1 and 2?

I agree with AWOL. Also, even if it did work, it seems like you're trying to print the ASCII value for 0 and 1, which are non-printing. Maybe this will help you see what's going on:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  int msg[10] ;
  msg[0] = 65;      // ASCII for 'A'
  msg[1] = 66;      // 'B'

const char* a = (char *) &msg[0];      
const char* b = (char *) &msg[1];

Serial.println(a);
Serial.println(b);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Also, the statement

int[10] msg;

is not a proper definition in C.

I see ... so I don't that the 1 is ascii value . Now how can I conver int 1 to "1" please advice

econjack:
I agree with AWOL. Also, even if it did work, it seems like you're trying to print the ASCII value for 0 and 1, which are non-printing. Maybe this will help you see what's going on:

void setup() {

// put your setup code here, to run once:
  Serial.begin(115200);
  int msg[10] ;
  msg[0] = 65;      // ASCII for 'A'
  msg[1] = 66;      // 'B'

const char* a = (char ) &msg[0];     
const char
b = (char *) &msg[1];

Serial.println(a);
Serial.println(b);
}

void loop() {
  // put your main code here, to run repeatedly:

}




Also, the statement



int[10] msg;




is not a proper definition in C.

What are you trying to do?

I got a solution

      char a[5];
      sprintf(a, "%d", msg[0]);
      char b[5];
      sprintf(b, "%d", msg[1]);
       
      char test[20];
      strcpy(test, a);
      strcat(test, ",");
      strcat(test, b);
      strcat(test, "\0");
      mclient.publish("topic1/sensorAck",test);

If you're going to use sprintf, cut it down

      char test[20];
      sprintf(test, "%d,%d", msg[0], msg [1]);

Edit: sprintf_P would be even better.

Oh thank mann :slight_smile: