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.

putting char or int data in string......

Status
Not open for further replies.

jit_singh_tara

Full Member level 6
Joined
Dec 22, 2006
Messages
325
Helped
9
Reputation
18
Reaction score
4
Trophy points
1,298
Location
Delhi , India
Activity points
4,293
Dear friends ,

I need to put some variables (unsigned char , unsigned int , unsigned long int ) type into a string which i will transmit via uart.

For example :

if a(unsigned int) = 987; // value will not exceed 999
b(unsigned long int) = 89997; // value will not exceed 99999

string has to be like : "#S98789997$" //#S has to be the header and $ the footer in the string

if the data value is less :
i.e if a = 45;
b = 783;
then the string should have zeroes in place of blank spaces i.e
string should be like : "#S04500783$"

please suggest what logic should be implemented to do the same .I am using pic mcu , mikroc 5.2.0 compiler .
Basically i need to put variables in the string with zeroes .
 

You could do this in a fairly straight forward way with an 'sprintf' call. However, you don't say which PIC MCU you are using. The 'printf' family of functions takes a lot of FLASH and can be slow at runtime.
If this is you only requirement that uses the 'printf' functions then you are much better off using the functions that would be calls anyway such as 'itoa' and some of the other string handling functions.
The problem with printf and the like is that the format string has to be interpreted at runtime. If the format string changes, especially in unpredictable ways (e.g. it is typed in by a user) then printf is the best way to go. However if the format string is fixed (as it typically is) then you can save a whole lot of time by just calling the underlying functions yourself with the appropriate parameters - you do the 'parsing' once and not each time the function is called.
Also the printf functions need to bring in every possible formatting function as it does not know at runtime which will be required. Therefore you end up with a lot of code being linked in that is never called. For the PIC18 and smaller memory MCUs, this can mean that you can't fit more than a trivial program into FLASH. PIC24 (and dsPIC33) and PIC32 MCU's generally have more memory but the runtime cost is still there.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top