1

I have these input variables:

 uint16 temperature = 0x1f12;
 uint8 array[8] = {0,0,0,...0}

And I want to have

array8[0] = '1';
array8[1] = 'f';
array8[2] = '1';
array8[3] = '2';
array8[4] = '\0';
array8[5] = '\0';
array8[6] = '\0';
array8[7] = '\0';

However, for memory problems (I'm working with microcontrollers!) I need to avoid functions such as sprintf, printf, puts, etc.

How should I do?

Best regards,

menchopez
  • 65
  • 2
  • 7

3 Answers3

2

Use recursion to determine 1 hex digit at a time.

// print digit at the end and return the next address
static char *itohexa_helper(char *dest, unsigned x) {
  if (x >= 16) {
    dest = itohexa_helper(dest, x/16);
  }
  *dest++ = "0123456789abcdef"[x & 15];
  return dest;
}

char *itohexa(char *dest, unsigned x) {
  *itohexa_helper(dest, x) = '\0';
  return dest;
}

int main(void) {
  char array[8];
  uint16_t temperature = 0x1f11;
  puts(itohexa(array, temperature));
  puts(itohexa(array, 0));
  puts(itohexa(array, 0x1234567));
  puts(itohexa(array, UINT_MAX & 0xFFFFFFF));
}

Output

1f11
0
1234567
fffffff
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

This code uses only 8 additional bytes in stack(int i, j).

int i;
for (i = 0; i < 8; i++) {
    array[7 - i] = temperature % 16;
    temperature /= 16;

    if (temperature == 0)
        break;
}

if (i == 8)
    i--;

int j;
for (j = 0; j <= i; j++)
    array[j] = array[7 - i + j];
for (j = i + 1; j < 8; j++)
    array[j] = 0;

for (j = 0; j < 8; j++)
    if (array[j] < 10)
        array[j] += '0';
    else
        array[j] += 'a' - 10;

This code first converts temperature = 0x1f12 to array[8] = { 0, 0, 0, 0, 1, 15, 1, 2}.

Then shifts the elements of array so that it becomes array[8] = { 1, 15, 1, 2, 0, 0, 0, 0 }.

And then converts the numbers to corresponding characters: array[8] = { '1', 'f', '1', '2', '0', '0', '0', '0' }.

Note also that this if condition

    if (i == 8)
        i--;

is never met, since break condition always suffices in the first for loop, even for temperature >= 0x10000000. It's just there in the hope that it might help someone understand this code.

nglee
  • 1,913
  • 9
  • 32
  • With `temperature = 0x123`, this answers forms array `"12300000"`, certainly not OP's goal. – chux - Reinstate Monica Jun 15 '17 at 15:42
  • @chux I think that's what OP wants.. – nglee Jun 15 '17 at 15:46
  • OP's rely to [this](https://stackoverflow.com/questions/44549385/integer-conversion-to-hexadecimal-string-in-c/44569241?noredirect=1#comment76096549_44549385) was ["123"](https://stackoverflow.com/questions/44549385/integer-conversion-to-hexadecimal-string-in-c/44569241?noredirect=1#comment76114556_44549385), although OP did accept your answer, hmmm. – chux - Reinstate Monica Jun 15 '17 at 15:47
  • Sorry for my C level... I wanted what @nglee posted. Thank you for help everyone. – menchopez Jun 16 '17 at 07:55
0
int convert()
{
 uint16_t temperature = 0x1f11;
 uint8_t array[8] = {0,0,0,0,0,0,0,0};

 int i = 0;

 for(i = 0; i < 8; i++){
    array[i] = (0xf000 & temperature) >> 12 ;
    temperature <<= 4;
    printf("array[%d] = %x\n", i ,array[i]);
 }
 return(0);
}
skr
  • 2,146
  • 19
  • 22