This is what I use for that sort of function. char ntoh (int n) { return ("0123456789ABCDEF" [n & 0x0f]); }
void raw_uart_puthex8 (int val) { raw_uart_write (ntoh (val >> 4)); raw_uart_write (ntoh (val)); } On Tuesday 21 October 2003 08:21 am, Pedro Zorzenon Neto wrote: > 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); > } > > > ------------------------------------------------------- > This SF.net email is sponsored by OSDN developer relations > Here's your chance to show off your extensive product knowledge > We want to know what you know. Tell us and you have a chance to win $100 > http://www.zoomerang.com/survey.zgi?HRPT1X3RYQNC5V4MLNSV3E54 > _______________________________________________ > Mspgcc-users mailing list > Mspgcc-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/mspgcc-users