|
-
Oct 20th, 2001, 03:48 PM
#1
Thread Starter
I wonder how many charact
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?
-
Oct 20th, 2001, 03:54 PM
#2
Member
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.
-
Oct 20th, 2001, 03:58 PM
#3
Thread Starter
I wonder how many charact
Yeah that great for const char.... but say
i have...
char g;
g = '5';
how do i get the integer value (5) from g?
-
Oct 20th, 2001, 04:07 PM
#4
Thread Starter
I wonder how many charact
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....
-
Oct 20th, 2001, 04:12 PM
#5
Member
-
Oct 20th, 2001, 06:16 PM
#6
transcendental analytic
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.
-
Oct 20th, 2001, 09:37 PM
#7
Hyperactive Member
I think kedaman's way is the better way if your number is always a single digit. Faster also.
-
Oct 21st, 2001, 10:10 AM
#8
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.
-
Oct 21st, 2001, 11:29 AM
#9
Addicted Member
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;
}
-
Oct 21st, 2001, 11:48 AM
#10
transcendental analytic
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.
-
Oct 21st, 2001, 11:54 AM
#11
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|