CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Digits lost in sprintf string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AJEllisuk



Joined: 03 Oct 2007
Posts: 4

View user's profile Send private message

Digits lost in sprintf string
PostPosted: Fri Jun 20, 2014 6:10 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 6:50 pm     Reply with quote

might be a compiler bug, please tell us the version of your compiler.

hth
jay
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 7:08 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jun 21, 2014 3:00 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Jun 21, 2014 6:07 am     Reply with quote

Have you tried using %04LX ?

L is for long integer which is 16 bits in CCS
AJEllisuk



Joined: 03 Oct 2007
Posts: 4

View user's profile Send private message

PostPosted: Sun Jun 22, 2014 3:48 pm     Reply with quote

hello guys,

Thank you for your replies. Using "%04LX" solved the problem.

Andrew
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group