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

A little help with printf()

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



Joined: 12 Dec 2008
Posts: 7

View user's profile Send private message

A little help with printf()
PostPosted: Wed Apr 01, 2009 6:55 am     Reply with quote

Hi all,

I am trying to output to the serial port a word (2bybte) with out any formatting. So if I have a variable with 64(dec) when I send it out to hyper terminal I will get nul@(ascii)

nul=0
@=64

My best bet but did not work how I expected
int16 X;
X=64;
printf("%s",X);
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Apr 01, 2009 7:10 am     Reply with quote

%s will try and display a string pointed to by the var (X) in this case. X does not actualy point to a string so you will get strange results.

You are trying to display 2 chars, the first is the upper byte of X and the second is the lower byte of X. you could do this like
Code:
printf("%c%c", X >> 8, X);

BUT you will have a problem because the ascii char for 0 (X >> 8) is an unprintable character, you will get anything from nothing displayed to a strange char, you wont see the value 0.

So we need to know what you are trying to do so we can offer you a better option. You could print the values
Code:
printf("%d%d", X >> 8, X & 0xFF);

There are lots of other options for you as well.

Your best option is prob
Code:
putc(X >> 8);
putc (X & 0xFF); // putc(X);

will prob work but I will leave you to try.
SimpleGuy



Joined: 12 Dec 2008
Posts: 7

View user's profile Send private message

PostPosted: Wed Apr 01, 2009 7:43 am     Reply with quote

thank you i am all set now.
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