I cannot, for the life of me, figure out how to convert from a object with a datatype char to an object with a datatype of int.
Anyone with any information, ideas, or know-hows, let me know.
Thanks.
I cannot, for the life of me, figure out how to convert from a object with a datatype char to an object with a datatype of int.
Anyone with any information, ideas, or know-hows, let me know.
Thanks.
1) char and int are not objects. They are primitives.
2) characters will automatically be converted (by sign extension) to ints as needed. For example:
char ch = 32;
int i = ch; // i = 32
3) To explicitly convert between primitive types, use the cast operator like this:
int i = (int) ch;
Casting is not needed if you are moving from a lower precision to a higher precision datatype -- char will be promoted to int automatically. Casting *is* needed to move from a higher precision to a lower precision, so to go from int to char you must cast.
To put a fine point on it, I disagree with point 1 made by The V.
While I agree char and int are not objects, but rather data types, and indeed they are primitives, they are also classes, just as surely as any class you or I may make. All the operators, etc. are listed in the libraries/header files that come with the compiler/IDE. You can not change them like you can your own or someone elses classes/methods, but they are there.
If what you want is to convert a char that has numbers in it like "123445" to a int that will end up like 123445 then include the stdlib library. the command int atoi(char *word) does what you want. eg
#include <stdlib.h>
void main(void)
{
char number[10] = "123456789";
int result = 0;
result = atoi(number); //result is now 123456789
}
You could also use these to change to different types of numbers:
atoi: converts string to int type.
atol: converts string to long type.
atof: converts string to float type.
...
Well, to split hairs:Originally posted by Unregistered
To put a fine point on it, I disagree with point 1 made by The V.
While I agree char and int are not objects, but rather data types, and indeed they are primitives, they are also classes, just as surely as any class you or I may make. All the operators, etc. are listed in the libraries/header files that come with the compiler/IDE. You can not change them like you can your own or someone elses classes/methods, but they are there.
Primitive data types (int, char, etc.) aren't classes, and variables aren't objects -- a variable is an instance of a primitive type, and an object is an instance of a class. Primitives are not defined in any headers, but are actually directly understood by the compiler itself.
The best argument as to why a primitive is not a class is that you cannot derive a class from a primitive data type; with any real class, you can derive a child class. Further, the primitives in C++ are the same, to the compiler, as C primitives, and C has no classes.
Personally I like C#, strong type checking. There are more built in types (even a 128-bit floating point) and you are guranteed that the type will be the same size on all computers because any code done in C# requires the .NET framework.
So what does MS pay you to hang around a C Board promoting C#???
C/C++ isn't owned by MS ...nuff said, + C++ is better designed than anything than MS will ever be able do accomplish.
Actually, MS did quite a good job with .NET's events (aka delegates).C++ is better designed than anything than MS will ever be able do accomplish.
- lmov