Results 1 to 11 of 11

Thread: converting char type to int

  1. #1

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    converting char type to int

    I want to convert a char type '9' to its real integer value, 9, and not the ascii code, so how do i do this?

  2. #2
    Code:
     #include <stdlib.h>
     #include <stdio.h>
    
     int main(void)
     {
        int n;
        char *str = "12345.67";
    
        n = atoi(str);
        printf("string = %s integer = %d\n", str, n);
        return 0;
     }
    The atoi() function.

  3. #3

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yeah that great for const char.... but say

    i have...

    char g;
    g = '5';

    how do i get the integer value (5) from g?

  4. #4

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    well somehow i managed to pull it off...

    int s;
    char e = '5';
    char g = '0';
    s = e-g;

    that works for some strange reason....

  5. #5
    Like this:
    Code:
    int s = atoi(q)

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    char c;
    c = '5';
    int i=c-48;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    I think kedaman's way is the better way if your number is always a single digit. Faster also.
    I'm a VB6 beginner.

  8. #8
    jim mcnamara
    Guest
    The standard way in C to get an integer value from a string is the ascii to integer function atoi().

    Don't go messing with char artihmetic operations. Plus, the default for most compilers is signed char, which will also get you in even more trouble with char arithmetic operations.

  9. #9
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    nemaroller run this code and you see why it works, and why shouldn't do it that way.

    Code:
    #include <iostream.h>
    
    void main()
    {
    	char a = '5';
    	char b = '0';
    	int c = a - b;
    	cout << (int)a << endl
    		 << (int)b << endl
    		 << c << endl << endl;
    
    	
    	c = a + b;
    
    	cout << (int)a << endl
    		 << (int)b << endl
    		 << c << endl;
    
    
    }

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by jim mcnamara
    The standard way in C to get an integer value from a string is the ascii to integer function atoi().

    Don't go messing with char artihmetic operations. Plus, the default for most compilers is signed char, which will also get you in even more trouble with char arithmetic operations.
    f**k the standard, C++ is lowlevel as much as highlevel, and chars aren't C-strings if you stand by your definitions. char is as much a integer as a C-string, depends in what context you use it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    besides, atoi will f**k you if next byte in memory isn't a nullchar
    if you want to be explicit about datatypes:

    char c;
    c = '5';
    int i=(int)c-48;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width