Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

sprintf() missing characters

Status
Not open for further replies.

gravi

Full Member level 2
Joined
May 30, 2006
Messages
128
Helped
10
Reputation
20
Reaction score
10
Trophy points
1,298
Location
Hyderabad, India
Activity points
2,452
memset sprintf

Hi
This is Ravi and working on Pic 18LF8722,MPLAB18 v3.00 compiler

static unsigned char PacketBuff[50];
memset(PacketBuff, 0, strlen(PacketBuff));
sprintf(PacketBuff,"<RESET BOR='%d' POR='%d' WDT='%d'/>",BORCount,PORCount,WDTCount);
Putsusart(PacketBuff);

result:
<RESET BOR='34', POR= '46' WDT='56'/>
<RESET BOR='34', POR= '46' WDT='56'/>
<RESET BOR=' ', POR= '46' WDT='56'/>
<RESET BOR='34', POR= ' ' WDT='56'/>
<REET BOR='34', POR= '46' WDT='56'/>

I observed sometimes that when I am trying to put the string on the hyperterminal, I have blank spaces in the buffer.
I am actually trying to print a packet data which is in User understandable format...I am seeing the spaces in between.

can anybody help me out on this....


Can anyone tell me why this is caused.

Thanks in adavance
Ravi
 

you should not use strlen(PacketBuff) because at this time the content
of PackBuff is undefined


use

sizeof(PacketBuff)
 

What aProgrammer said. strlen() will return a garbage count for uninitialized memory (needs to find an ASCII string terminated with a zero).
 

What aProgrammer and jhbbunch said and -
Why use memset? Sprintf() will terminate the string with a null so you don't need to clear the array. And Putsusart is only sending up to the null.
I'd just remove the memset line.
 

Yeah, what newelltech said. You can't be too efficient when it comes to C programming. Redundant or superfluous calls can be disiastrous when coding for small memory models like 8 bit micros. I can remember using sprintf() and printf() calls routinely in the same DOS program with TurboC 3.0. I found out real quick just how much memory I was wasting when I tried the code in an 8051. Lesson learned: use sprintf() only.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top