Arm Mbed and Pelion Device Management support forum

Creating an unsigned char from multiple char and int space separated

I try to make a program to encrypt data in AES. The encryption part works well, there is no problem, but i have an issue with a char variables. I am new in C++ and is looks a bit different to C so i ask how to add some alphanumeric characters to an existing char?
My code looks like:

int r1 = rand()%167; //give 23 as answer
char data1[7] = { "12.56 }; //data read by the sensor max -122.99
char data2[8] = {“15786.58”}; //data from second sensor max 99999.99
char data3[8] = {“26352.22”}; //same as second sensor but read from serial port
unsigned char msg[0x90] = { data1[7] . " " . data2[8] . " " . data[3] }; <-Here is a problem, i need to have “12.56 15786.58 26352.22”

I can encrypt the only if a write:

unsigned char msg[0x90] = {“12.56 15786.58 26352.22”};

How to create a space separated char from multiple int and chars on my example?

You can try to use sprintf. For example:

unsigned char msg[0x90];

sprintf((char*)msg, "%s %s %s", data1, data2, data3);

It still working with char but what if i need also with an int, have tested with:

sprintf((char*)msg, “%s %s %s %s”,r1, data1, data2, data3);

With error, because r1 is int. How to ad a value from this int also into a char?

You need to learn how to format.
%s is for string. Try %d. It is for decimal number. There are a lot more format specifiers.

Tnx, i will RTFM now :slight_smile: