Conversion from char to int!!

This is a discussion on Conversion from char to int!! within the C Programming forums, part of the General Programming Boards category; Hi to all, I need some help regarding the following: I have to enter a number of integers separated by ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    22

    Conversion from char to int!!

    Hi to all,

    I need some help regarding the following:

    I have to enter a number of integers separated by commas (for example: 4,25,3,100) and I need to get each number entered and put it in an array. The problem is that this series of numbers and commas is taken as a string. I'm not able to perform the conversion from char to int. How do you suggest I go about it? Is there a ready-made function in C that I can use? Or should I write my own?

    Warm regards,
    Visham

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    fgets() and sscanf() is an option in this case. There are quite a few ways of going about it. See the FAQ article of this website that refers to reading a number from the user.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    why don't you use something like
    scanf("%d,%d,%d,%d", &a, &b, &c, &d);

    EDIT: sorry, forgot about the part that says you want to input it into an array. But I think this would work:

    scanf("%d,%d,%d,%d", a[0], a[1], a[2], a[3]);
    Last edited by Abda92; 08-03-2007 at 04:19 PM. Reason: mistake in answer

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You could also use a combinaison of strtok() and atoi()/sscanf().

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wonder if the char was a letter, like 'b', and you made it into a capital 'B' letter, and then if you remembered that all char's are represented by numbers in their (usually) ASCII chart, and thought "what would happen if I subtracted 'A' from my new 'B'?".

    Of course, there are library functions for this, as well. But just for fun, what do you think the value would be here, for integer_variable:

    integer_variable = 'B' - 'A';

    ?


    If you subtracted the difference between 'A' and 'a', from a letter's ASCII value, would that be the same as using toupper()?

    Would 'R' == 'r' - ('r' - 'R')?

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    Hi to all,

    I am not using a standard scanf to accept input here..it's actually dissecting the options in an IPTables rule. The user has to enter sth like this:

    IPTables -A ......--lbw 4,5,10,6

    I have to be able to remove the numbers 4, 5, 10 and 6 separately and use them as integers. When you enter such a rule at console, it is taken as a string (we make use of optarg). That's why I needed the conversion from char to int. I tried with strtol and it worked. atoi also works.

    I wasn't aware of these functions...many thx to all those who have posted..it helped to quickly come to what I needed.

    Warm regards,
    Visham

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quote Originally Posted by visham View Post
    Hi to all,

    I am not using a standard scanf to accept input here..
    sscanf() then

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 07:14 AM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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