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; What could be wrong with this code?: Code: int arr[10]; char arr2[10]; for (int i=0; i<10; i++) { arr[i]=(int)arr2[i]; } ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    Conversion from char to int?

    What could be wrong with this code?:

    Code:
    int arr[10];
    char arr2[10];
    for (int i=0; i<10; i++)
    {
            arr[i]=(int)arr2[i];
    }
    When I output the supposedly converted array, it's outputs some crazy numbers.
    ???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    For one thing, you have not initialised arr2.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    any random characters including spaces.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Okay, so what is so crazy about the numbers? Don't you get the (presumably) ASCII values as you intended?

    You might want to post the smallest and simplest compilable program that demonstrates the problem. Post your sample input, expected output, and actual output.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Try this , you will get to know the problem.

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
    int arr[10];
    char arr2[10]="abcdefghi"; // Initialize the array
    int i;
    for (i=0; i<10; i++)
    {
            arr[i]=(int)arr2[i]; // Converting to int .(ASCII value)
    }
    
    for (i=0; i<10; i++)
    {
            printf("===> %d\n",arr[i]);
    }
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by karthigayan
    Try this , you will get to know the problem.
    Sorry, but your program does not explain the problem. On the other hand, you are not TheUmer
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    arr2 is only initialised with 9 values in the last example by the way

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by rogster001
    arr2 is only initialised with 9 values in the last example by the way
    No, it is correctly initialised with 10 values, including the null character.

    Personally, I thought that this might be a "convert digit to integer" problem, but the "any random characters including spaces" comment makes me think otherwise.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    @karthigayan:

    I ran your code and it gives the 'segmentation fault' error.

    @laserlight:

    Input string is: [p 1 (a tab) 0]

    And it outputs: 80 49 9 48 0 0 0 0 0 0

    I want to get 1 and 0 as integers.
    ?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by TheUmer
    Input string is: [p 1 (a tab) 0]

    And it outputs: 80 49 9 48 0 0 0 0 0 0

    I want to get 1 and 0 as integers.
    ?
    Ah. In that case, you do want to convert a digit to an integer. Since you want to leave those characters that are not digits untouched, it implies that you need some logic in the loop, e.g., check if arr2[i] >= '0' && arr2[i] <= '9'. If it is, then you subtract '0' from arr2[i] to get the integer value. Otherwise, you just assign the value as per normal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Alright. The problem is solved to some extend. But a little more problem:

    For the given string [P 1 (tab) 0] it outputs fine: 1 0
    But when I have a string [P 2 (tab) 34] it outputs: 2 3 (no four.)

    What could be the problem?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by TheUmer View Post
    Alright. The problem is solved to some extend. But a little more problem:

    For the given string [P 1 (tab) 0] it outputs fine: 1 0
    But when I have a string [P 2 (tab) 34] it outputs: 2 3 (no four.)

    What could be the problem?
    You only read in a single character, which is 3.

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    I wonder why you never reply with complete answer? How can I solve this problem?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Code:
    int arr[10];
    char arr2[10];
    for (int i=0; i<10; i++)
    {
          if (arr2[i]>=0 && arr2[i]<=9)
          {
                arr[i]=(int)arr2[i]-48;
          }
    }
    for a single 34, it takes 3 but not 4.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 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