Due - Converting a 16 bit integer to hex - Solved

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));


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.


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:
  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));