I have a string of numbers, need to convert to ints (ATOI?)

This is a discussion on I have a string of numbers, need to convert to ints (ATOI?) within the C Programming forums, part of the General Programming Boards category; I tried the atoi function but it keeps going until it's not an int. for example Code: char s[5] = ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    I have a string of numbers, need to convert to ints (ATOI?)

    I tried the atoi function but it keeps going until it's not an int.

    for example

    Code:
    char s[5] = "12a12";
    
    int p=atoi(&s[0]);
    would give me 12, but I need one.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    s[0] - '0' will give the int value for characters in the range '0' - '9'
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    thnkx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 04:14 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21