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

Long integer to string

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



Joined: 15 Sep 2003
Posts: 75

View user's profile Send private message

Long integer to string
PostPosted: Fri Feb 03, 2006 5:31 am     Reply with quote

I would like to convert a long integer v=2345 into a string called str=“2.345”.

I know how to strip v into single digits like integers v[0]=2, v[1]=3, v[2]=4, v[3]=5 by using division and modulus operators.

Could someone please tell me how I can convert the above v[] array of integers into str=”2.345”

I prefer a small function as opposed to standard library functions to save ROM space.
Benjamin



Joined: 11 Jan 2006
Posts: 21
Location: Quebec (Canada)

View user's profile Send private message Visit poster's website

PostPosted: Fri Feb 03, 2006 9:12 am     Reply with quote

Well the easy way would have been using sprintf:

Code:

sprintf(string,"%lu",long);
// or in your case
sprintf(str,"%f",v/1000);


But if you really want to do this manually:
you have:
v[0]=2, v[1]=3, v[2]=4, v[3]=5
Since a string is in ASCII format (you can find the ASCII table on the internet), you want this:

str[0] = 50, str[1] = '.', str[2] = 51, str[3] = 52, str[4] = 53
All you have to do is add 48 to an interger to get the ASCII code.
You should try both methods to see if this really takes up less ROM.
Benjamin



Joined: 11 Jan 2006
Posts: 21
Location: Quebec (Canada)

View user's profile Send private message Visit poster's website

PostPosted: Fri Feb 03, 2006 9:15 am     Reply with quote

P.S. D'ont forget that a string should normally finish with a NUL caracter (0), so str[5] = 0. This might depend on how you will use the string. Also make sure your table is large enough to receive this extra caracter.
TL



Joined: 15 Sep 2003
Posts: 75

View user's profile Send private message

PostPosted: Fri Feb 03, 2006 2:55 pm     Reply with quote

Thanks Benjamin for your help. I'll try your suggestions.
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