This is a C++ example:
CODE
#include <iostream>
using namespace std;
int isInt( char c )
{
if ( c < '0' || c > '9' ) return -1;
return c - '0';
}
int main()
{
char str[] = "1234567890";
cout << "Converting the ASCII value of the char's in the string\n\n"
<< str
<< "\n\ninto their corresponding integer values:\n\n";
for( int i = 0; str[i] != 0; ++i )
cout <<"x" << str[i]-'0' << "x ";
printf( "\n\nOr...\n\n" );
for( int j, i = 0; str[i] != 0; ++i )
{
j = isInt( str[i] );
cout <<"x" << j << "x ";
}
cin.get();
return 0;
}
This example is in C:
CODE
#include <stdio.h>
int isInt( char c )
{
if ( c < '0' || c > '9' ) return -1;
return c - '0';
}
int main()
{
char str[] = "1234567890";
printf( "Converting the ASCII value of the char's in the string\n\n%s\n\n", str );
printf( "into their corresponding integer values:\n\n");
int i;
for( i = 0; str[i] != 0; ++i )
printf( "x%dx ", str[i]-'0' );
printf( "\n\nOr...\n\n" );
int j;
for( i = 0; str[i] != 0; ++i )
{
j = isInt( str[i] );
printf( "x%dx ", j );
}
getchar();
return 0;
}
This post has been edited by David W: 16 Oct, 2008 - 04:26 AM