Using strtol: convert string to long
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
long x;
char *string = "-1234567abc", *remainderPtr;
x = strtol( string, &remainderPtr, 0 );
cout << "The original string is \"" << string
<< "\"\nThe converted value is " << x
<< "\nThe remainder of the original string is \""
<< remainderPtr
<< "\"\nThe converted value plus 567 is "
<< x + 567 << endl;
return 0;
}
Related examples in the same category