convert int to uint8_t values

Hi,

I have two integer values say a and b. The values of a and b range between 0 to 9999. Both of the values have to be passed into uint8_t array[4];
The array format
uint8_t array[4] = { 0x00, 0x00, //value of a should come here (hex format)
0x00, 0x00}

0x00 represents the hex format. (i.e., if a = 65535 is 0xffff)

I tried using sprintf to hold the value but I was not able to get an exact conversion. The program I did works for int but not uint8_t.
Any help on this would be great.
Thanks an regards
Niranjan

Please provide the code you're working with. You seem to be confused. All numbers the processor works with are binary. Hex, decimal, etc are just human-readable representations of the binary numbers. 'sprintf()' produces ASCII character strings, which is probably not what you want.

There's a few ways to do this, one of which is using a union:

typedef union int_byte 
{
    int intVal;
    byte byteVal[sizeof(int)];  
};

int_byte
    uIB;
char
    szString[20];

uint8_t 
    array[4];

void setup() 
{    
    Serial.begin(9600);
    
    uIB.intVal = 2587;
    sprintf( szString, "%d -- 0x%02X%02X\n", uIB.intVal, uIB.byteVal[1], uIB.byteVal[0] );
    Serial.print( szString );
    array[3] = uIB.byteVal[1];
    array[2] = uIB.byteVal[0];

    uIB.intVal = 9987;
    sprintf( szString, "%d -- 0x%02X%02x\n", uIB.intVal, uIB.byteVal[1], uIB.byteVal[0] );
    Serial.print( szString );
    array[1] = uIB.byteVal[1];
    array[0] = uIB.byteVal[0];

    for( int i=3; i>=0; i-- )
    {
        sprintf( szString, "%02X", array[i] );
        Serial.print( szString );
        
    }//for
    
    Serial.println();
    
}//setup

void loop() 
{
    
}//loop

Hi,

Yes, here is the code which I am trying:

int a = any number between (0 - 9999);
int b = any number between (0 - 9999);

uint8_t adData1[4] = {
/* a / 0x00,0x00,
/
b */ 0x00,0x00
}
P.S. I realized i am spliting integer (2 bytes) into uint8_t one byte and sending it. But I dont know the syntax in this case.

This is how the code format is. The adData1 values are displayed in decimal in my other device(mobile phone). I cannot modify that since I don't have the code base for that.
It's a huge file. That's why I could not share it.
Regards
Niranjan

Niranjan_ravi:
}
P.S. I realized i am spliting integer (2 bytes) into uint8_t one byte and sending it. But I dont know the syntax in this case.

You can use the union like in reply #2.

You can use bit shifting and bit masking.

int a = 1234;
int b = 5678;

uint8_t data[4] = { (a >> 8), (a&0xFF), (b>>8), (b&0xFF)};

Or use the Arduino functions highByte() and lowByte.

int a = 1234;
int b = 5678;

uint8_t data[4] = { highByte(a), lowByte(a), highByte(b), lowByte(b)};

Niranjan_ravi:
P.S. I realized i am spliting integer (2 bytes) into uint8_t one byte and sending it.

How are you "sending it"? What is the signature of the function you're calling? You may not need to "convert" anything. If you're talking about RF69, LORA, nRF24L01+, etc., then you only need a properly cast pointer.

Thanks a ton.
I was able to get it working.
Regards
Niranjan