Hi Gene,

  I don't think this is a bug in sprintf. When a char is converted to
int, the MSB bit is copied to all other bits, this way:
     10101010 -> 1111111110101010
  If the MSB were 0, then:
     01010101 -> 0000000001010101

  Try this (I did not test... don't know if it will work as
expected):

void write0_byte_to_hex(char * s) {
   char sresult[3];
   sprintf(sresult, "%02X", ((int) s[0]) & 0xff);
   uart_enq(&uart0, sresult);
}

  Anyway, I think the code of sprintf is very big, if you are using it
only for printing to hex. I implemented some simpler and smaller code
than sprintf, but only useful when you need "%02X".

void raw_uart_puthex8 (int val) {
  int i;
  i = val >> 4;
  i &= 0x0f;
  if (i < 10)
    {
      i += '0';
    }
  else
    {
      i += 'A' - 10;
    }
  raw_uart_write(i);

  i = val;
  i &= 0x0f;
  if (i < 10)
    {
      i += '0';
    }
  else
    {
      i += 'A' - 10;
    }
  raw_uart_write(i);
}

Reply via email to