How it's possible to give hexadecimal value to array's element ?

Hello dears,

Please I need a help.

in my code, I have the message that I want to send, char variable, for example:

char variable = {0x11,0x10,0x00,0x02,0x00,0x00,0x00,0x00};

This message is for Rs-485.

I have a sensor with an int value:
int sensorValue1 = 1000;

now, I want to convert 1000 for Hex, so the answer is 3E8 according to the following code:

char variable = {0x11,0x10,0x00,0x02,0x00,0x00,0x00,0x00};
int sensorValue1 = 1000;
int x, y;
void setup()
{
Serial.begin(9600);

delay(10);
}
void loop()
{
String x_st(x);
String y_st(y);
x = sensorValue1/16;
x_st = String (x,HEX);
y= sensorValue1%16;
y_st = String (y,HEX);
variable[3]= x_st[0]; // here what I should write to have an answer equal to 3E instead of 3 ??????
Serial.println(variable[3]);
delay (1000);
}
Thanks.

You already have the number in x. Forget the string. Why co,nvert to string just to convert right back.

variable[3] = x;

Delta_G:
You already have the number in x. Forget the string. Why co,nvert to string just to convert right back.

variable[3] = x;

Ten bits into eight doesn't go

I am confused as to what you are trying to put in the variable array. It is declared as type char so each element would normally be a char. Please can you give an example of what the variable array should look like after putting the hexadecimal number in it ? You certainly can't get 3E8 into a single element of the array.

Thanks , it is solved
Delta_G

thanks

So how do you get your 3E?

With my conversions you will get a 3 and a E8

byte variable[] = {0x11, 0x10, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00};
int sensorValue1 = 1000;
void setup()
{
  Serial.begin(9600);

  variable[3] = sensorValue1 & 0xFF;
  variable[4] = sensorValue1 >> 8;

  *((unsigned int*)&variable[5]) = sensorValue1; // this generates a warning

  Serial.print("3+4 ");
  Serial.print(variable[3], 16);
  Serial.print(" ");
  Serial.println(variable[4], 16);
  Serial.print("5+6 ");
  Serial.print(variable[5], 16);
  Serial.print(" ");
  Serial.println(variable[6], 16);
}
void loop() {}

I just put
x = sensorValue/256;
y = sensorValue%256;
variable[2]= x;
variable[3]= y;

Tanta:
what I should write to have an answer equal to 3E instead of 3 ??????

The point I wanted to make is that splitting 0x3E8 in bytes,
wont give bytes with the content 0x3E 0x80, but 0x03 and 0xE8.

If you are looking at hexadecimal values of odd length (3 in the above case),
you have to insert a zero in front of the pattern (at least in your mind),
to make it easy to see the byte grouping (at least for me).

wrong 0x1234567 correct 0x01234567, wrong 0x3E8 correct 0x03E8