I'd like to fix the length of the variable in sprintf.

Hi, all

I take a variety of sensor values, convert them into hexadecimal values, put them in an array.
and I've got I running it on serial print function.
The length of continuously output data has changed, causing a problem.
It turns out that the hexadecimal value should come in four digits, but it was in eight digits.

I put &0000ffff on a temporary basis, so I fixed four output digits.
I'd like to know if it's the right way.

I'm using many sprintf function in my code,
If i call many serialEvent function or another function. then values of Sprintf begin to change.
I took only some part in my code.

int ADC01_OFF = 0;
int ADC02_OFF = 0;
int ADC03_OFF = 0;
int ADC04_OFF = 0;
int16_t ADS1115_str[4];

char temp[500] = {0};

sprintf(temp,"%04X%04X%04X%04X%04X%04X%04X%04X",
ADC01_OFF&0x0000ffff,ADC02_OFF&0x0000ffff,ADC03_OFF&0x0000ffff,ADC04_OFF&0x0000ffff,
ADS1115_str[0],ADS1115_str[1],ADS1115_str[2],ADS1115_str[3]);
SerialUSB.print(temp);

Does anyone have an idea?

Please post code that compiles, runs and demonstrates the problem. The snippet is useless, and is not the source of your reported problem.

It turns out that the hexadecimal value should come in four digits, but it was in eight digits

This happens with Arduino when you try something like the following, where x is improperly interpreted as a long int.

int x=-1;
Serial.println(x,HEX);
char temp[500] = {0};

sprintf(temp,"%04X%04X%04X%04X%04X%04X%04X%04X",

What Arduino are you using? Is it truly wise to reserve 500 bytes for a 32 byte string?

Post the whole code. If the rest of it treats memory the way this part does then it’s no wonder that it doesn’t work.

Sorry for late reply,
It was finished this project few days ago, and I check it now.

void SensorDisplay(void){
    byte error;
    int8_t address1;
    address1 = ads1.ads_i2cAddress;

    
    Wire.beginTransmission(address1);

    error = Wire.endTransmission();

    
    if (error == 0 )
    {
        ADS1115_str[0] = ads1.Measure_SingleEnded(0);   
        ADS1115_str[1] = ads1.Measure_SingleEnded(1);        
        ADS1115_str[2] = ads1.Measure_SingleEnded(2);
        ADS1115_str[3] = ads1.Measure_SingleEnded(3);
        ADS1115_str[4] = ads2.Measure_SingleEnded(0);
        ADS1115_str[5] = ads2.Measure_SingleEnded(1);
        ADS1115_str[6] = ads2.Measure_SingleEnded(2);
        ADS1115_str[7] = ads2.Measure_SingleEnded(3);
        
    }
    else
    {
        SerialUSB.print("ADS1115 Disconnected! : ");        
    }
    
}

I added code more, (the sensor function).
When it has a problem, ADS1115_str[0] value become "FFFFFFFF" value. ( 8 digits)
ADS1115_str value must be "FFFF" value. ( 4 digits)

If I try to fix as "ADS1115_str[0]&0x0000ffff" code
The code seems to work fine.

I don't know this is good code or not

When it has a problem, ADS1115_str[0] value become “FFFFFFFF” value. ( 8 digits)
ADS1115_str value must be “FFFF” value. ( 4 digits)

If it is still defined as an int16_t then it can’t possibly be 8 hex digits long. It may print that way because of the way print works. But it can’t possibly really be that.

print promotes int to long before it prints it and your value got sign extended. That’s all.