View previous topic :: View next topic |
Author |
Message |
AJEllisuk
Joined: 03 Oct 2007 Posts: 4
|
Digits lost in sprintf string |
Posted: Fri Jun 20, 2014 6:10 pm |
|
|
Hi
I am creating a string to transmit over a serial port. The code is mostly fine, but I have a problem with "%04X" in sprintf; it loses the first 2 digits. For example if I want to print 0x0697 to a string, only 0x97 is written.
Below is a snippet of my code:
Code: |
int8 txbuf[20];
int16 adc = 0;
int16 crc=0xFFFF;
int8 button;
adc = read_adc();
button = input(PIN_B0);
crc = update_crc(crc, adc);
crc = update_crc(crc, button);
sprintf(txbuf, "@%04X%02X%04X\r", adc, button, crc);
|
Can someone please explain why the %04 format specifier is dropping the first 2 digits.
Kind regards
Andrew |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Fri Jun 20, 2014 6:50 pm |
|
|
might be a compiler bug, please tell us the version of your compiler.
hth
jay |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Fri Jun 20, 2014 7:08 pm |
|
|
I haven't tried sprintf in CCS, but printf requires %04Lx on most of my boards |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Jun 21, 2014 3:00 am |
|
|
Yes, I know the CCS manual suggests what you are doing SHOULD give the result you want.
I've not tested it, but I suspect jeremiah is probably correct.
What he says makes sense, there is likely an error in the CCS manual.
Mike |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Sat Jun 21, 2014 6:07 am |
|
|
Have you tried using %04LX ?
L is for long integer which is 16 bits in CCS |
|
|
AJEllisuk
Joined: 03 Oct 2007 Posts: 4
|
|
Posted: Sun Jun 22, 2014 3:48 pm |
|
|
hello guys,
Thank you for your replies. Using "%04LX" solved the problem.
Andrew |
|
|
|