0

I don't know how to write a code that convert a byte array to a char array in C++ (using an Arduino board) and publish mqtt. I tried to search but I don't understand.

Example

byte Code[3] = {0x00 ,0x01 , 0x83};
char byteTochar[3];
    
for (int i = 0; i <= 2; i++) {
    Serial.printf("%d", Code[i]);
    Serial.println();
    
    client.publish("publish/data", byteTochar[i]);
}

Error message

converting to 'String' form initializer list would use explicit constructor 'String::String'(unsigned char, unsigned char)'
1
  • 1
    This is not C, i guess. Commented Aug 11, 2015 at 18:22

1 Answer 1

4

Its actually do c, ArduinoPlayGround http://playground.arduino.cc/Main/Printf.

However, you can just use casting for each element:

char h = (char)Code[i];
Sign up to request clarification or add additional context in comments.

5 Comments

sorry i don't understand use byte h = (byte)Code[i]; for convert to char array ?
My bad, i changed it. do it for each of the array elements throug a loop and youll get an array of byte. Or you can just convert right before passing the argument.
Thank answer, i will to try :)
i try write code for apply data not work, console not show data message. pastebin.com/VkbyYg3U
Please explain what exactly this code should do, cause you passed the wrong params to itoa and you didnt use the char, so i dont get what are you trying to do. However, try to initiate the buffer (str) and read this: cplusplus.com/reference/cstdlib/itoa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.