Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,408 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,472 people online right now. Registration is fast and FREE... Join Now!




How to determine numerical values of a character

 
Reply to this topicStart new topic

How to determine numerical values of a character

oxyron
15 Oct, 2008 - 10:49 AM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2008
Posts: 1

How do you write a code that determines the numerical value of a char containing a digit in the range '0' ... '9' to an integer with the appropriate value? Like as an example, if ch has the value '3', the value 3 will be assigned to an int variable.

I know this is probably simple, but I am very new to C++.

Help?
User is offlineProfile CardPM
+Quote Post

David W
RE: How To Determine Numerical Values Of A Character
15 Oct, 2008 - 11:18 AM
Post #2

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
Updated C++ example to "C++ style" ... Please see next post:

This post has been edited by David W: 16 Oct, 2008 - 01:52 AM
User is offlineProfile CardPM
+Quote Post

David W
RE: How To Determine Numerical Values Of A Character
16 Oct, 2008 - 02:12 AM
Post #3

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
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
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: How To Determine Numerical Values Of A Character
16 Oct, 2008 - 02:20 AM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
or probably I would use atoi() .
User is offlineProfile CardPM
+Quote Post

David W
RE: How To Determine Numerical Values Of A Character
16 Oct, 2008 - 02:30 AM
Post #5

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
QUOTE(AmitTheInfinity @ 16 Oct, 2008 - 03:20 AM) *

or probably I would use atoi() .


Yes you could ... but first you would have to form a C string from each char.

If you are interested in the int value of a C string (already formed), like

char myCstr[] = "12345";

... then ... you could just pass that to 'atoi'

int x = atoi( myCstr );

This post has been edited by David W: 16 Oct, 2008 - 03:46 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:15PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month