Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 94409
  • Last Modified:

Convert String to Integer

How can I convert a standard string to an integer?
0
rossryan
Asked:
rossryan
  • 2
  • 2
2 Solutions
 
jkrCommented:
There are several methods, the easiest being 'atoi()' the most flexible IMHO (in terms of error checking) being 'strtol()':


/* ATOF.C: This program shows how numbers stored
 * as strings can be converted to numeric values
 * using the atof, atoi, and atol functions.
 */

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

void main( void )
{
   char *s; double x; int i; long l;

   s = "  -2309.12E-15";    /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

   s = "7.8912654773d210";  /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

   s = "  -9885 pigs";      /* Test of atoi */
   i = atoi( s );
   printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

   s = "98854 dollars";     /* Test of atol */
   l = atol( s );
   printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}

Output

atof test: ASCII string:   -2309.12E-15   float:  -2.309120e-012
atof test: ASCII string: 7.8912654773d210   float:  7.891265e+210
atoi test: ASCII string:   -9885 pigs      integer: -9885
atol test: ASCII string: 98854 dollars      long: 98854

/* STRTOD.C: This program uses strtod to convert a
 * string to a double-precision value; strtol to
 * convert a string to long integer values; and strtoul
 * to convert a string to unsigned long-integer values.
 */

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

void main( void )
{
   char   *string, *stopstring;
   double x;
   long   l;
   int    base;
   unsigned long ul;
   string = "3.1415926This stopped it";
   x = strtod( string, &stopstring );
   printf( "string = %s\n", string );
   printf("   strtod = %f\n", x );
   printf("   Stopped scan at: %s\n\n", stopstring );
   string = "-10110134932This stopped it";
   l = strtol( string, &stopstring, 10 );
   printf( "string = %s", string );
   printf("   strtol = %ld", l );
   printf("   Stopped scan at: %s", stopstring );
   string = "10110134932";
   printf( "string = %s\n", string );
   /* Convert string using base 2, 4, and 8: */
   for( base = 2; base <= 8; base *= 2 )
   {
      /* Convert the string: */
      ul = strtoul( string, &stopstring, base );
      printf( "   strtol = %ld (base %d)\n", ul, base );
      printf( "   Stopped scan at: %s\n", stopstring );
   }
}


Output

string = 3.1415926This stopped it
   strtod = 3.141593
   Stopped scan at: This stopped it

string = -10110134932This stopped it   strtol = -2147483647   Stopped scan at: This stopped itstring = 10110134932
   strtol = 45 (base 2)
   Stopped scan at: 34932
   strtol = 4423 (base 4)
   Stopped scan at: 4932
   strtol = 2134108 (base 8)
   Stopped scan at: 932

0
 
jkrCommented:
BTW, regarding the 'std::string' issue - all you have to do is calling the 'c_str()' member of that class before passing it to any of these functions, e.g.

std::string str = "1234";

int i;

i = atoi(str.c_str());

char* pc;

i = (int) strtol(str.c_str(), &pc, 10);

if ( NULL != *pc) {

    // conversion error, probably incorrect numerical format
}
0
 
rstaveleyCommented:
Arguably, the most C++-ish way to do it is using std::istringstream:
--------8<--------
#include <iostream>
#include <sstream>

int main()
{
      std::istringstream istr("1234");
      int i;
      istr >> i;
      std::cout << "Value is " << i << '\n';
}
--------8<--------
0
 
poojasureshkumarCommented:
Hi.
I want to convert string "&H0" into long variable.Can any one help me?
I tried with long.parse(string) and Convert.ToInt64(string)..which will not work because the string argument must contain numneric string value.

Can any one help me .

Advance Thanks,
suresh
0
 
rstaveleyCommented:
You are in the wrong place, suresh, I think you want http:/Programming/Programming_Languages/Dot_Net/VisualC_PLUS_PLUS_DOT_NET/ if you are doing Managed C++ / Whidbey / CLI or whatever we're supposed to call it.

Though, &H0 looks like Visual Basic .NET - see http:/Programming/Programming_Languages/Dot_Net/ for all the .NET goodies.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now