how do i convert a string to an integer??

This is a discussion on how do i convert a string to an integer?? within the C Programming forums, part of the General Programming Boards category; please help. i need to convert a string (with a numeric value) to an integer. how do i do that ...

  1. #1
    Unregistered
    Guest

    how do i convert a string to an integer??

    please help.
    i need to convert a string (with a numeric value) to an integer. how do i do that in c?
    thanks a lot.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    int i;
    char * string = "1024";
    i = atoi(string);
    printf("%d", i);
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    5
    But how come when I try to do this

    char *string="ARE9";
    int key=atoi(string);
    printf("%d", key);
    cout<<endl;

    it only gives me a 0?

  4. #4
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    Are you trying to create a hash? I don't have much experience with this, but try playing around with bit ops etc..

    here's an example (probably not good, just off the top of my head..)

    Code:
    int examplehash(char *string)
    {
    	int i = 0, hash = 0;
    
    	while(string[i])
    	{
    		hash += string[i];
    		hash ^= string[i];
    		++i;
    	};
    
    	return hash;
    };
    Give it a try, play around with it. Do a google search.

    *blah, fixed code tag + missing increment
    Last edited by ggs; 12-11-2001 at 04:07 PM.
    .sect signature

  5. #5
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi wtyyip!

    What did you expect ?

    Code:
    DESCRIPTION
           The atoi() function converts the initial  portion  of  the
           string  pointed  to  by nptr to int.  The behaviour is the
           same as
    
                  strtol(nptr, (char **)NULL, 10);
    
           except that atoi() does not detect errors.
    ... so you should use strtol.

    In fact... you C library is friendly enough to give you 0, it could return you any value it likes (at least with the ISO90 standard).

    alex
    Last edited by alex; 12-11-2001 at 04:11 PM.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    5
    I guess my question now is: how to I convert character such as ABC back to their ASCII number.
    0=48, A=65, B=66 etc...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int x = 'a';

    Pretty simple stuff.

    printf( "a is %d", 'a' );

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. convert from integer to string
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2006, 09:28 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 04:03 PM

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