Thread: char to int

  1. #1
    Unregistered
    Guest

    Exclamation char to int

    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.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    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.

  3. #3
    Unregistered
    Guest
    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.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    stdlib I think

    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.
    ...

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    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.
    Well, to split hairs:

    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.

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    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.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    15

    hehe Troll King

    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.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    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).
    - lmov

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .NET and C# are open standards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM