Thread: Simple casting question

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Simple casting question

    I am trying to create a function that receives a char and returns the ascii value. Can I meerly cast the intput char as an int and return it?

    Thanks

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    You could do the following:

    Code:
    #include <stdio.h>
    
    int main( int argc, char ** argv ){
    
    //this segfaults if you don't specify a character, obviously.
    
      char ch = *argv[1];
    
      printf("%X\n", (unsigned short)ch);
    
      return 0;
    }
    You should be fine casting it as well, really all you need is the one byte.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by thgilber View Post
    I am trying to create a function that receives a char and returns the ascii value. Can I meerly cast the intput char as an int and return it?
    That would be a no-op. On a system that uses ASCII, a char is it's ASCII value.

    Yes, you can just cast it anywhere that you want to move the data between variables of different types, but that usually isn't necessary as a simple assignment statement will do it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    When you say
    a function that receives a char and returns the ascii value. Can I meerly cast the intput char as an int and return it?
    ...it's not clear.

    What do you mean by? "receives a char" and "input char"
    Where EXACTLY is this coming from? ...a function parameter? ...user input? ...outer space?

    What do you mean "returns the ascii value"? ...a function return? ...a print to screen?

    Also, when you say "receives a char and returns the ascii value" it is also confusing as far a data terminology.
    They are the same!

    All values of type char (0-255 decimal) are on the ascii chart.
    (Values from 32 to 126 decimal are the standard English alpha-numeric and punctuation characters)

    So again we are left to guess what you are trying to do.

    How about posting at least a function prototype next time?
    Last edited by HowardL; 05-18-2012 at 08:16 AM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by HowardL View Post
    Also, when you say "receives a char and returns the ascii value" it is also confusing as far a data terminology.
    They are the same!
    iMalc touched on this, but they are not necessarily the same. They are only the same on systems that use ASCII or ASCII-compatible encodings like UTF-8. Systems that use, e.g., EBCDIC would need to map those values to their ASCII counterparts.

    All values of type char (0-255 decimal) are on the ascii chart.
    Also not quite true. Technically ASCII is only values 0-127. Often values 128-255 are called "extended ASCII", but they're not true ASCII and there is nothing standard about it.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    values 128-255 are called "extended ASCII", but they're not true ASCII
    Right, thanks for correcting that faux pas.
    I should limited my comments on ascii.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question about pointers and casting
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2008, 02:25 PM
  2. Simple casting problem
    By micke_b in forum C Programming
    Replies: 23
    Last Post: 11-11-2007, 02:40 PM
  3. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  4. very simple problem......casting.......
    By incognito in forum Windows Programming
    Replies: 1
    Last Post: 03-10-2004, 09:25 PM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM