Decimal to Hex conversion, "0x00" format

I don't know how to fill a byte string with the format "0x00".
I need to create LedAddress[0]=0x02 from an int variable Address
LedAddress[0]= (Address, HEX); Out of order

How can I create this variable correctly?
Thanks

#include <RS485.h>


void setup() 
  {
  RS485.begin(9600, HALFDUPLEX);    // Si fos RS422 auries de fer el setup() daquesta manera RS485.begin(9600, FULLDUPLEX)
  Serial.begin(9600); 
  delay(1000);
  Serial.println("********************************");
  Serial.println("Ver 0_0 Convertir Decimal a Hex ");
  Serial.println("********************************"); 
  }

void loop() 
  {
  EncenLed(1,2,1);  //adrec 2, Color Verd, Encen
  delay(2000);   
  Serial.println();
  
  }

void EncenLed(int Adreca, int ColorLed, int OnOff)
   {
   byte LedAddress[8] = {0x02, 0x05, 0x00, 0x02, 0xFF, 0x00, 0x2D, 0xC9};  //02 05 00 02 FF 00 2D C9 -> Encen Verd
        //LedAddress = {0x02, 0x05, 0x00, 0x02, 0x00, 0x00, 0x6C, 0x39};   //02 05 00 02 00 00 6C 39 -> Apaga verd 


   //Encen Verd
   Serial.println("Encent");
   LedAddress[0]= (Adreca, HEX);  //NO FUNCIONA
   //LedAddress[0]=0x02; 
   LedAddress[1]=0x05; LedAddress[2] =0x00; LedAddress[3] =0x02; 
   LedAddress[4] =0xFF; LedAddress[5] =0x00; LedAddress[6] =0x2D; LedAddress[7] = 0xC9; 
   for (size_t i=0; i < sizeof LedAddress; i++) 
     {
     RS485.write(LedAddress[i]);
     }    
   delay(5000);
   
   //Apagaga Verd 
   Serial.println("Apaga");
   LedAddress[0]=0x02; LedAddress[1]=0x05; LedAddress[2] =0x00; LedAddress[3] =0x02; 
   LedAddress[4] =0x00; LedAddress[5] =0x00; LedAddress[6] =0x6C; LedAddress[7] = 0x39; 
   for (size_t i=0; i < sizeof LedAddress; i++) 
     {
     RS485.write(LedAddress[i]);
     }
   
   }
LedAddress[0]= Address;

if you where PRINTING the value out the format (ie, Hex, dec, bin, etc) would matter.

put it seems you are acually sending the value out, so there human readable formats are irrelevant!

just use the int value for your LED address....

hope that helps...

Hex decimal and binary - are the representations only for humans.
It is absolutely doesn't matter for controller, all the lines below are equivalent:

LedAddress[0]=0x02;
LedAddress[0]= 2;
LedAddress[0]= 0b00000010;

So you don't need any 'conversion" to store hex byte to the value.

LedAddress[0]=0x02 --> is what I need to have.
I have an "int variable Address = 2"
How can I create LedAddress[0] from an "int" , which has the HEX format "0x00"?

 LedAddress[0]  = Address;

wrong.
You still not understand that LedAddress[0] hasn't such format "0x00"

thanks, it doesn't work because it is not "0x00" format.

Did you try it? If so, post the error message you see.

Or did you assume it would not work?

there is no such format for integer data. Forget it.
If your code not works - look for errors in the code, not in fictitious formats.

Show your whole code.

Yes I have tried it.


int Address = 2;
  
LedAddress[0]= (Address, HEX); //OUT OF ORDER
LedAddress[0]= Address; //OUT OF ORDER
LedAddress[0]=0x02; //IF IT WORKS

What the type and value of the Address variable?

Thanks for the help. I put all the code at the beginning. The "EncenLed" function.
As you can see, if I write the variable directly, it works for me. The problem I have is because the variable with parameters is not created.

void EncenLed(int Adreca, int ColorLed, int OnOff)
   {
   byte LedAddress[8] = {0x02, 0x05, 0x00, 0x02, 0xFF, 0x00, 0x2D, 0xC9};  //02 05 00 02 FF 00 2D C9 -> Encen Verd
        //LedAddress = {0x02, 0x05, 0x00, 0x02, 0x00, 0x00, 0x6C, 0x39};   //02 05 00 02 00 00 6C 39 -> Apaga verd 


   //Encen Verd
   Serial.println("Encent");
   LedAddress[0]= (Adreca, HEX);  //NO FUNCIONA
   //LedAddress[0]=0x02;   //SI FUNCIONA
   LedAddress[1]=0x05; LedAddress[2] =0x00; LedAddress[3] =0x02; 
   LedAddress[4] =0xFF; LedAddress[5] =0x00; LedAddress[6] =0x2D; LedAddress[7] = 0xC9; 
   for (size_t i=0; i < sizeof LedAddress; i++) 
     {
     RS485.write(LedAddress[i]);
     }    
   delay(5000);
   
   //Apagaga Verd 
   Serial.println("Apaga");
   LedAddress[0]=0x02; LedAddress[1]=0x05; LedAddress[2] =0x00; LedAddress[3] =0x02; 
   LedAddress[4] =0x00; LedAddress[5] =0x00; LedAddress[6] =0x6C; LedAddress[7] = 0x39; 
   for (size_t i=0; i < sizeof LedAddress; i++) 
     {
     RS485.write(LedAddress[i]);
     }
   
   }

Your LedAddress[8] array contains 8 bytes. Why do you assign only first one?

are you meant this code:

?

Your code is not complete. There is no setup() and loop() functions, so impossible to see how you using your "EncenLed" function

It is an integer, which can range from 0 to 255. But for the peripheral, these are HEX addresses from 0 to FF

#include <RS485.h>


void setup() 
  {
  RS485.begin(9600, HALFDUPLEX);    // Si fos RS422 auries de fer el setup() daquesta manera RS485.begin(9600, FULLDUPLEX)
  Serial.begin(9600); 
  delay(1000);
  Serial.println("********************************");
  Serial.println("Ver 0_0 Convertir Decimal a Hex ");
  Serial.println("********************************"); 
  }

void loop() 
  {
  int a, b, c = 0;

  /*
  a = random(1,4);
  b = random (0,2);
  c = random (0,3);  // 0 vermell, 2 verd
  EncenLed(a,c,b);
  */
  EncenLed(2,2,1);  //adrec 2, Colr Verd, Encen
  delay(2000);   
  Serial.println();
  
  }

void EncenLed(int Adreca, int ColorLed, int OnOff)
   {
   byte LedAddress[8] = {0x02, 0x05, 0x00, 0x02, 0xFF, 0x00, 0x2D, 0xC9};  //02 05 00 02 FF 00 2D C9 -> Encen Verd
        //LedAddress = {0x02, 0x05, 0x00, 0x02, 0x00, 0x00, 0x6C, 0x39};   //02 05 00 02 00 00 6C 39 -> Apaga verd 


   //Encen Verd
   Serial.println("Encent");
   LedAddress[0]= (Adreca, HEX);  //NO FUNCIONA
   //LedAddress[0]= Adreca;  //NO FUNCIONA
   //LedAddress[0]=0x02;   //SI FUNCIONA
   LedAddress[1]=0x05; LedAddress[2] =0x00; LedAddress[3] =0x02; 
   LedAddress[4] =0xFF; LedAddress[5] =0x00; LedAddress[6] =0x2D; LedAddress[7] = 0xC9; 
   for (size_t i=0; i < sizeof LedAddress; i++) 
     {
     RS485.write(LedAddress[i]);
     }    
   delay(5000);
   
   //Apagaga Verd 
   Serial.println("Apaga");
   LedAddress[0]=0x02; LedAddress[1]=0x05; LedAddress[2] =0x00; LedAddress[3] =0x02; 
   LedAddress[4] =0x00; LedAddress[5] =0x00; LedAddress[6] =0x6C; LedAddress[7] = 0x39; 
   for (size_t i=0; i < sizeof LedAddress; i++) 
     {
     RS485.write(LedAddress[i]);
     }
   
   }  

 
  

It does not give an error, simply, it is not the desired address and the peripheral does not turn on.

0-255 and 0- FF are two absolutely the same integer ranges.

According to your description - it is a single byte. But your address array needs eight bytes. How do you think to construct the address from single byte?

Of course.
After assigning the first byte to "0x02" to the empty address you will get: all zeros in all address fields other than first:

LedAddress[8] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

To construct correct address, you have to assign ALL eight bytes of array.
It is because your code like this works:

Your problem is not in hex format, but in fact that you work with address incorrectly.