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 support@ccsinfo.com

Problem with Sprintf function

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Problem with Sprintf function
PostPosted: Wed Apr 25, 2018 5:27 pm     Reply with quote

Hi someone can help me with my code:

Code:
#include <18F4620.h>
#fuses HS,NOWDT,WDT32768,PROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)// RS232 Estándar

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int j,i;
char Cabecera1[]={0x00, 0x11, 0x00, 0x0D, 0x81};
char Buffer[10];

void main(){   

   j=0;
   for (i=0; i<5; i++){
      sprintf(Buffer[j+i],"%02X",Cabecera1[i]);            
   }
   
 while(TRUE);

}


The Sprintf function is not working... What is my error?
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 25, 2018 5:38 pm     Reply with quote

You need to post what you expected it to do as well as what it actually did.
I don't know what it should do...however from a 'general observation'...

1) you should NOT use the 'protect' fuse until the final version of the code is 100%

2) you should ALWAYS add 'ERRORS' to the Use RS232(...options...)

3) the variable 'j' is always 0, so not needed.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Wed Apr 25, 2018 5:53 pm     Reply with quote

Ok Temtronic, but because the function do not work?
jeremiah



Joined: 20 Jul 2010
Posts: 1314

View user's profile Send private message

PostPosted: Wed Apr 25, 2018 6:07 pm     Reply with quote

pilar wrote:
Ok Temtronic, but because the function do not work?


Again, what value do you expect in "Buffer" and what value do you actually see?

Just a cursory glance indicates that you are not using the variable j at all, so I would expect only the first 6 characters of buffer to be filled, with the loss of 4 characters. Also, if you intend it to be a string of 10 characters, you did not leave room for a null character, so you would not get what you expect anyways.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Wed Apr 25, 2018 6:23 pm     Reply with quote

I hope to find in Buffer the values of Cabecera1, but when I run the program the Buffer remains empty without data....
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 25, 2018 6:38 pm     Reply with quote

OK, I have to ask how you're looking at the contents of the 'buffer' ?

Jay
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Apr 25, 2018 6:40 pm     Reply with quote

Why not just STRCPY ()?

you have a Index on the target array which to me seems odd.
And an index on the source array so you would be only printing that location

Quote:
sprintf(Buffer[j+i],"%02X",Cabecera1[i]);

Change to:
Code:

sprintf(Buffer,"%s",Cabecera1);

_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 12:38 am     Reply with quote

The big problem is buffer[j+i]. This is _not_ a RAM address.....

In C, an address is generated by using '&'. There is a shortcut, in that the name of an array, is treated as it's address, but here he is selecting an element in the array. The result will be that the code will actually write to an address dependant on what happens to be in element 'i' of the array. Result garbage.....

Now the printf outputs two characters each time, but he moves forwards by one character. Better syntax to put the hex into the buffer:
Code:

char Buffer[11]; //enough space for the five pairs of characters & null


void main(){   

   j=0;
   for (i=0; i<5; i++){
      sprintf(Buffer + j +(i*2),"%02X",Cabecera1[i]);   
      //Will give an address of the start of buffer, moving forwards
      //2 characters each time.
   
     //Or
     sprintf(& Buffer[j +(i*2)],"%02X",Cabecera1[i]);     
     //again gets the address   
   }
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 2:17 pm     Reply with quote

Hi Ttelmah,

Thank for clarifying my error ... this work Very Happy
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