Thread: char conversion

  1. #1
    Unregistered
    Guest

    char conversion

    Is there anyway of converting a char to it's ascii value other than specifiy that a=97, b=98 etc?

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    char mychar = 'a';
    int myvalue = static_cast<int>(mychar);

    or

    int myvalue = static_cast<int>('a');
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Use static_cast<int>(yourChar).

    -----
    char myChar = 'a';
    int numRep = static_cast<int>(myChar);
    -----

    Kuphryn

  4. #4
    Unregistered
    Guest
    thanx for that Just something else...what if it were a string. Would I be able to do the same or would I need to read each char and then convert it?

  5. #5
    Unregistered
    Guest
    Sorry....do I need a specific .h file to use static_cast??

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Use the same approach.

    Kuphryn

  7. #7
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by Unregistered
    Sorry....do I need a specific .h file to use static_cast??
    Nope.
    If it's not compiling due to an identifier error, check your project. If you are under C, casts are done differently.

    (type) expression;

    instead of

    static_cast<type>(expression)

    so,

    (int) 'a'; //for C
    static_cast<int>('a'); //for C++
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. Fixing Errors
    By brietje698 in forum Networking/Device Communication
    Replies: 9
    Last Post: 12-10-2007, 11:17 PM
  3. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM