Results 1 to 5 of 5

Thread: Convert string into int in C

  1. #1
    Join Date
    Apr 2008
    Posts
    45

    Convert string into int in C

    Hello,
    Can you tecll me how to to convert an integer variable into a string variable in C? any help would be greatly appreciated. Thank you .

  2. #2
    Join Date
    Mar 2008
    Posts
    198

    Re: Convert string into int in C

    ยป char *itoa(int value, char *buffer, int radix);

    Converts an integer value to a null-terminated string using the specified radix and stores the result in the given buffer. If radix is 10 and value is negative the string is preceded by the minus sign (-). With any other radix, value is always considered unsigned.buffer should be large enough to contain any possible value: (sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.

    Here is the example:

    Code:
    #include <stdlib.h>	// for itoa() call
    #include <stdio.h>	// for printf() call
    
    int main() {
    	int num = 123;
    	char buf[5];
    
    	// convert 123 to string [buf]
    	itoa(num, buf, 10);
    
    	// print our string
    	printf("%s\n", buf);
    
    	return 0;
    }

  3. #3
    Join Date
    Dec 2008
    Posts
    1,108

    Re: Convert string into int in C

    Refer to following thread to get more help:

    How to convert string to int

    Hope this helps.
    The difference between stupidity and genius is that genius has its limits. - Albert Einstein

    What we think, we become (Please don't think you are a superhero and don't try to fly)

    "SUCCESS IS NOT A DESTINATION , IT'S A JOURNEY"

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Convert string into int in C

    Below is example code for Converting string to int, double and long:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int i;
      double d;
      long l;
    
      i = atoi("1");
      l = atol("11111111");
      d = atof("11111.11111");
    
      printf("%d %ld %f", i, l, d);
    
      return 0;
    }

  5. #5
    Join Date
    Dec 2011
    Posts
    1

    Re: Convert string into int in C

    hey heres a better way ... ( the original way, without using library functions)
    you can convert a char type into int type by:
    here ch must b a single digit character(0-9) (its not a string)
    num=ch-48; // shifting the ASCII value 48 units back.

    now use this approach in a loop to convert the whole string as:

    int chartoint(char *k)
    {
    int i;
    int l;
    int j;
    int t=0;
    l=strlen(k);

    cout<<endl<<l<<endl;
    for(i=0;i<l;i++)
    {
    j=k[i]-48;
    t=t+j*(int)pow(10,l-i-1);
    }
    return t;
    }


    pass it the string ... say (123) and it will return 123 in integertype;

Similar Threads

  1. Convert XML string into DOM
    By GreatThinker in forum Software Development
    Replies: 6
    Last Post: 22-07-2010, 09:48 AM
  2. How to convert string into int in Java
    By Zool in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 12:41 PM
  3. How to convert string to int
    By Zavier in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 06:24 PM
  4. Convert a string in decimal
    By FlayoFish in forum Software Development
    Replies: 3
    Last Post: 23-04-2009, 12:18 PM
  5. Convert a string to bytes
    By $tatic in forum Software Development
    Replies: 4
    Last Post: 18-02-2009, 02:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,173,303.31931 seconds with 17 queries