Due - Converting a 16 bit integer to hex - Solved

Hi
I'm a bit new to some of this and have a problem which is really left me scratching my head.

int16_t advtemp = ((int16_t)(cTemp * 100.0));
//  
  int16_t advhum = ((int16_t)(humidity * 100.0));

  sprintf(AdvDataCmd,"AT+LEADDATA=%s%s%04X%04XAABB%s\r",Flags,ManData,advtemp,advhum,Name);

I'm working with a 16 bit temperature/humidity sensor and a Due board and I need to send the result (advtemp and advhum) as a pair of 16 bit hex values (no more no less always 4 characters) in a string. My code works fine when advtemp and advhum are positive and I see answers such as "0BC4". However if ,say, advtemp is negative then the hex representation returned is 32 bit e.g "FFFFF134". I am only interested in the last 4 characters. Is there a way of instructing the Due to only return "F134". My values can never exceed the range of a 16 bit integer.

Thanks
Mike

You COULD generate a string that contains "FFFFF134", and then just use the last 4 characters...

Try this :

void setup() {
  // put your setup code for core 0 here, to run once:
  Serial.begin(250000);
  int16_t temp = -2;
  printf(" the temp is = %hX\n", temp);
}

void loop() {

}

All printf() parameters are defined here:printf - C++ Reference

Ah, thank you. That works nicely
Just to be complete this is the modified code fragment

int16_t advtemp = ((int16_t)(cTemp * 100.0));
 
int16_t advhum = ((int16_t)(humidity * 100.0));

 
  sprintf(AdvDataCmd,"AT+LEADDATA=%s%s%h04X%h04XAABB%s\r",Flags,ManData,advtemp,advhum,Name);