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
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
You could do the following:
You should be fine casting it as well, really all you need is the one byte.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; }
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"
When you say...it's not clear.a function that receives a char and returns the ascii value. Can I meerly cast the intput char as an int and return it?
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.
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.
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.All values of type char (0-255 decimal) are on the ascii chart.
Right, thanks for correcting that faux pas.values 128-255 are called "extended ASCII", but they're not true ASCII
I should limited my comments on ascii.