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

Character array to long?

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



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

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

Character array to long?
PostPosted: Mon Oct 27, 2008 5:08 pm     Reply with quote

Hi everybody.

I've got a little problem. Through hyperterminal the user must be able to type in a number which then is used as setpoint for a PID algorithm.

The number can be anything between 1-1000

My code right now looks like this:

char string[10];
long frequency;

... lots of irrelevant code ...

gets(string);


Now! Let's assume the user typed 978. How do I convert the character array to a long? Should I maybe use an all different approach?

Thank you for your help!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 27, 2008 5:35 pm     Reply with quote

Consider that for common problems like this, the C language will
already have a solution -- it's the series of functions known as
"atoi" (ASCII to Integer). In this case, you want a 16-bit result,
so use the version of it called "atol" (ASCII to long). It's in the
CCS manual.
davidbue



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

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

PostPosted: Tue Oct 28, 2008 1:09 am     Reply with quote

Hi PCM programmer!

I'm so sorry. I forgot to say... I'd like to not use the atoil() as the stdlib.h eats up waaay to much memory when included. :-D

I'm looking for an alternative way to capture the number.

Sorry for the missing information, and thank you so much for your answer.

Best regards
David
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 28, 2008 1:16 am     Reply with quote

You can use a truncated version of it. See the adtol (ASCII Decimal
to Long) function in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=17563&highlight=adtol
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