converting char to int

This is a discussion on converting char to int within the C Programming forums, part of the General Programming Boards category; Hi everyone! I'm new to C, and I need to convert to a char into an integer. The char is ...

  1. #1
    Unregistered
    Guest

    converting char to int

    Hi everyone! I'm new to C, and I need to convert to a char into an integer. The char is a part of an array, which seems to be the problem. Apparently atoi() has to accept an array of chars, but that won't do for my project. I thought of using a switch statement, but that seems kind of "unprofessional". Anybody have any ideas?
    Thank you again!

  2. #2
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    I am not sure whether this is what you need ...

    Code:
    #include <stdio.h>
    
    int main (void)
    {
     char *str = "123456789";
     int num;
    
     while (*str)
     {
      sscanf (str, "%1d", &num);
    
      printf ("%d\n", num);
    
      str++;
     }
    
     return 0;
    }
    Last edited by PutoAmo; 04-19-2002 at 02:15 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm new to C, and I need to convert to a char into an integer
    This may or may not be what you need
    Code:
    char a = '5';
    int d = a - '0';
    /* d now contains the decimal equivalent of 5 */
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Just do be really picky and difficult
    Code:
    char a = '5';
    int d = a - '0';
    /* d now contains the decimal equivalent of 5 */
    Will only work on the ascii char set. The only reason I thought about that is because I read about it in K&R last night. I don't know about anything not using the ascii char set.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Just do be really picky and difficult
    I like it that way

    >Will only work on the ascii char set.
    Please excuse the following pedantry.
    /* K&R pp.43 */
    As we discussed in Chapter 1, the expression

    s&#91;i] - '0'

    gives the numeric equivalent of the character stored in s&#91;i], because the values of '0', '1', etc., form a contiguous sequence.

    Another example of char to int conversion is the function lower, which maps a single character to lower case for the ASCII character set.

    <snip code>

    This works for ASCII because the corresponding upper and lower case letters are a fixed distance apart as numeric values and each alphabet is contiguous -- there is nothing but letters between A and Z. This latter observation is not true of the EBCDIC character set, however, so this code would convert more than just letters in EBCDIC.
    /* K&R pp.22-23 */
    This particular program relies on the properties of the character representation of the digits. For example, the test

    if (c >= '0' && c <= '9') ...

    determines whether the character in c is a digit. If it is, the numeric value of that digit is

    c - '0'

    This works only if '0', '1', ..., '9' have consecutive increasing values. Fortunately, this is true for all character sets.
    So your statement that the code will only work on ASCII is not explicitly stated by K&R for a number conversion from char to int, in fact it could be interpreted that they even state that it will work on all character sets. I've personally never tried it on a machine that uses EBCDIC, but I have no reason to believe that it will not work since EBCDIC defines 0 through 9 as
    Code:
    hex  character
    ---  ---------
    F0     0
    F1     1
    F2     2
    F3     3
    F4     4
    F5     5
    F6     6
    F7     7
    F8     8
    F9     9
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    MM Actually your right. I was typing faster then my brain was thinking(imagine that*smile*) Do you know if EBCDIC is really used? Some of my programs are using conversations that are strictly based on the ascii chart but if this is bad practice I would like to know. I know that all the standard functions are supposed to be "safe" on that issue.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Do you know if EBCDIC is really used?
    Yes it is, mostly in IBM mainframes, so it's usually safe to assume ASCII if the program is going to remain on microcomputers. However, it's best to write portable code whenever you can on the off chance that some goofball decided to use a weird character set.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    thank you all for helping me. I have a question though, about putoAmo's code: what is 'str' refer to? Is it a pointer to a string literal? Do you have to allocate memory for it? Thank you again.

  9. #9
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    what is 'str' refer to? Is it a pointer to a string literal?
    Yup, the string actually looks like 123456789\0
    Do you have to allocate memory for it
    Nope, memory is allocated when it is declared like any other variable
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    thank you all for helping me. I have a question though, about putoAmo's code: what is 'str' refer to? Is it a pointer to a string literal? Do you have to allocate memory for it? Thank you again.
    Yep, it's a pointer to a string literal. When using string literals, you never have to allocate space for them. The compiler does it.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Unregistered
    Guest
    hmmmm...
    So how would I access the string pointed to by str, say if I wanted to print it? I wrote this but it just prints null:

    Code:
      const char s1[]="hello,my name is sam.";
      const char delimiters[]="., ";
      char *token;
      char *cp;
      strcpy(s1,cp);
      token=strtok(cp,delimiters);
      printf("%s",token);
    I tried using &token and *token, but it gave me runtime error.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    hmmmm...
    So how would I access the string pointed to by str, say if I wanted to print it? I wrote this but it just prints null:

    Code:
      const char s1[]="hello,my name is sam.";
      const char delimiters[]="., ";
      char *token;
      char *cp;
      strcpy(s1,cp);
      token=strtok(cp,delimiters);
      printf("%s",token);
    I tried using &token and *token, but it gave me runtime error.
    Of course it gives you errors. You're using strcpy completely wrong. Additionally, you just have pointers that have no space allocated for them, so even if you were trying to copy something some place, 'cp' has not allocated space.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    aoit

    ammm


    why not try

    int atoi(const char *str)


    e.g

    #include<stdio.h>
    #include<stdlib.h>

    main(){
    char num1[80],num2[80];

    printf("enter first number");
    gets(num1);
    printf("enter second");
    gets (num2);
    printf("the sum is %d", atoi(num1) + atoi(num2));
    return 0;
    }


    i hope this will work....
    " programming is 1% syntax and 99% logic "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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