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

printf() behavior

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



Joined: 16 Nov 2004
Posts: 8

View user's profile Send private message

printf() behavior
PostPosted: Mon Feb 21, 2005 12:52 pm     Reply with quote

I am trying to send 32 bit integer(let's say as hex) from hyperterminal to PIC 16f877.
For example, let's say I send 0x30303030( characters 0000) to PIC.
I use 4 getc statements to receive and store it in an array.
And use make32 statement to convert the received four 8 bit integers to 32 bit integer.
When I print that 32 bit integer back using format specifier"%8x", it gives 00000030.
When I use "%8Lx", it prints 000030303030.
This is wierd. I would like to know, why this occurs. I have attached the code snippet with this post. I hope it helps.
Thanks for any help
cartic
[code]
void rst_tmr2(void)

{

int imsb[4];

int i,j=0;
char ctrl;

long int ind=0,count=0;

i=4;

while(true)

{

if(kbhit())
{

ctrl=getc();

goto b;

}

}

b:
if(ctrl=='h')

{

putc(ctrl);

while(i>=1){

j=i-1;

if(kbhit()){

imsb[j]=getc();

printf("%2x",imsb[j]);

i--;

}

}

}

a:
while(true){

if(kbhit()){

ctrl=getc();

putc(ctrl);

}

if(ctrl=='l'){

putc(ctrl);

delay_us(960);

cnt=0;

putc('c');

set_timer2(0x00);

putc(ctrl);

setup_timer_2(T2_DIV_BY_4,0XFF,15);

channel=12;

display();

// ind=8;

goto l;

}
}

l:
MSB=make32(imsb[3],imsb[2],imsb[1],imsb[0]);

LSB=make32(cnt,0,0,0);//variable reading the timer 2 count

printf("%8x%8x",MSB,LSB);

enable_interrupts(global);

enable_interrupts(INT_TIMER2);


}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 21, 2005 1:20 pm     Reply with quote

You need to convert your data from ASCII format to binary. In other
words, you need to convert from a text string to an integer.

It's obvious you're a student. We're getting a lot of student questions
on here lately. This is not to disparage you. Everyone is a student
at some point, or many times. But I want to let you know something
about the C language. Almost anything that you want to do as a
student has already been thought of, and there are ways to do it.
For example, you need convert a numeric ASCII string to an integer.
What you should do, is scan the list of available functions in either the
CCS manual, or the help file. There is almost certainly going to be
a function that will do what you need. In your case, it's the "atoi32"
function. Look in the manual for information on this function.
Also note the atoi32() operates on a string. A string is a series of
ASCII characters, with the last byte set = 0. That's a binary zero,
not an ASCII zero. The final 0 byte tells string functions that "this is
the end of the string". When you read in that value with several getc()
statements, you need to put in a line of code to insert a 0 byte right
after the last ASCII byte. Then the atoi32() function will operate
properly.


Also, when you're posting code, you should not double-space every line.
Tto use the "Code" format button, you need to highlight all your code,
and then just click the "Code" button once. Then use "Preview" to check
that it's done properly before you press "Submit".
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Feb 21, 2005 1:47 pm     Reply with quote

remember that
%x prints an 8bit hex number
%Lx prints a 16bit hex number
cartic



Joined: 16 Nov 2004
Posts: 8

View user's profile Send private message

PostPosted: Mon Feb 21, 2005 3:18 pm     Reply with quote

Thanks for the replies, You are right, I am student doing my project using PIC C. I already browsed through the manual and know about the atoi function, but I did not use the atoi() function at all. I am just sending 32 bit hexadecimal number to PIC, make PIC receive it as integer and print it back.
I am not doing any type conversion at all.
But my doubt was why is it printing the same number in weird manner, when only the format specifier is changed from %8x to %8LX. when I send 30303030(32bit integer), if I print back as %8x or as %8LX, it should print the same answer, but with zeroes added or without the zeroes, but the entire answer should not differ, right.
using format specifier"%8x", it gives 00000030.
When I use "%8Lx", it prints 000030303030.

If you addressed the same point,can you pl. make it a bit more clear. The second reply was somewhat relevant but, I still did not get the expected reply or may the reply in manner in which I can understand.
thanks
cartic
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Feb 21, 2005 6:25 pm     Reply with quote

Hi cartic,

For your info, PCM programmer and Mark are two of the best contributors that
this board have and the answers they gave you are rigth in all sense, if you
don�t understand them, -without being offensive- did you think the possibility
that you will need to learn a little more C ? Confused

Reading your code it�s obvious that you are learning, it�s plenty of "goto" that,
-I agree it�s a valid C statement - I�m sure you will eliminate as soon as you
learn how to use another statements.

Quote:

When I print that 32 bit integer back using format specifier"%8x", it gives 00000030.


The compiler is doing what you coded. "%8x" means that you reserve a
print field of 8 digits to print one integer (30) x = Hex int

Quote:

When I use "%8Lx", it prints 000030303030.
This is wierd. I would like to know, why this occurs


The compiler is doing what you coded. "%8Lx" means that you reserve a
print field of 8 digits to print one Hex long int

From the help "printf":

Format:

The format takes the generic form %wt where w is optional and may be
1-9 to specify how many characters are to be outputted, or 01-09 to
indicate leading zeros or 1.1 to 9.9 for floating point.


Best wishes and read as much C code as you can understand. Very Happy


Humberto
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