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,

10
  • 1
    just shift & mask the values. Add '0' if between 0 and 9, else add 'A'-10. Commented Jun 14, 2017 at 15:53
  • else add 'a' - 10 which is 'W' Commented Jun 14, 2017 at 15:54
  • See stackoverflow.com/a/36968534/1477064 Commented Jun 14, 2017 at 15:56
  • Should array[] write leading 0 digits? Example: should temperature = 0x123 --> "123" or "0123" or it makes no difference? Commented Jun 14, 2017 at 19:22
  • 1
    Your edit breaks the existing answers. Also, array8 = '1f12' does not make any sense. Please rollback your question. Commented Jun 15, 2017 at 11:44

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

7 Comments

Not my downvote, but It generates the number in the reversed order.
@Jean-FrançoisFabre True. I 1st wrote it reversed as that makes the most sense, but then mis-read OP's desired order. Hmmm uint16 temperature = 0x89ab; would have been clearer. Will reverse.
@Jean-FrançoisFabre Fixed.
Alternative: ideone.com/wcpl10
@Stargateur Interesting.
|
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.

4 Comments

With temperature = 0x123, this answers forms array "12300000", certainly not OP's goal.
@chux I think that's what OP wants..
OP's rely to this was "123", although OP did accept your answer, hmmm.
Sorry for my C level... I wanted what @nglee posted. Thank you for help everyone.
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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.