View previous topic :: View next topic |
Author |
Message |
SimpleGuy
Joined: 12 Dec 2008 Posts: 7
|
A little help with printf() |
Posted: Wed Apr 01, 2009 6:55 am |
|
|
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); |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Apr 01, 2009 7:10 am |
|
|
%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. |
|
![](templates/subSilver/images/spacer.gif) |
SimpleGuy
Joined: 12 Dec 2008 Posts: 7
|
|
Posted: Wed Apr 01, 2009 7:43 am |
|
|
thank you i am all set now. |
|
![](templates/subSilver/images/spacer.gif) |
|