Well the title pretty much sums it up. I want to use something like asc("0") in C++, and want to make the program platform independent so don't want to use 48! Any help appreciated.
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
You can simply use single-quotes to make a character constant:
The character type is a numeric type, so there is no real need for
asc
andchr
equivalents.Here's a small example that prints out the character values of a string:
The output is:
In C and C++, if you use a character enclosed by
''
and not""
it means you are dealing with its raw binary value already.Now, in C and C++,
"0"
is a literal two byte null-terminated string:'0'
and'\0'
. (ascii 48 ascii 0)You can achieve what you want by using
var[0]
on a "" null-terminated string or use one of the conversion routines. (atoi()
in C,stringstream
lib in C++)You will get the ASCII value of a 0 character by writing: '0'.
Likewise 'char' for every char you need.