How do I convert a char
to an int
in C and C++?
相关问题
- 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
Well, in ASCII code, the numbers (digits) start from 48. All you need to do is:
C and C++ always promote types to at least
int
. Furthermore character literals are of typeint
in C andchar
in C++.You can convert a
char
type simply by assigning to anint
.It sort of depends on what you mean by "convert".
If you have a series of characters that represents an integer, like "123456", then there are two typical ways to do that in C: Use a special-purpose conversion like atoi() or strtol(), or the general-purpose sscanf(). C++ (which is really a different language masquerading as an upgrade) adds a third, stringstreams.
If you mean you want the exact bit pattern in one of your
int
variables to be treated as achar
, that's easier. In C the different integer types are really more of a state of mind than actual separate "types". Just start using it wherechar
s are asked for, and you should be OK. You might need an explicit conversion to make the compiler quit whining on occasion, but all that should do is drop any extra bits past 256.Presumably you want this conversion for using functions from the C standard library.
In that case, do (C++ syntax)
The expression
UChar( c )
converts tounsigned char
in order to get rid of negative values, which, except for EOF, are not supported by the C functions.Then the result of that expression is used as actual argument for an
int
formal argument. Where you get automatic promotion toint
. You can alternatively write that last step explicitly, likeint( UChar( c ) )
, but personally I find that too verbose.Cheers & hth.,
(This answer addresses the C++ side of things, but the sign extension problem exists in C too.)
Handling all three
char
types (signed
,unsigned
, andchar
) is more delicate than it first appears. Values in the range 0 toSCHAR_MAX
(which is 127 for an 8-bitchar
) are easy:But, when
somevalue
is outside of that range, only going throughunsigned char
gives you consistent results for the "same"char
values in all three types:This is important when using functions from ctype.h, such as
isupper
ortoupper
, because of sign extension:Note the conversion through int is implicit; this has the same UB:
To fix this, go through
unsigned char
, which is easily done by wrapping ctype.h functions through safe_ctype:This works because any function taking any of the three char types can also take the other two char types. It leads to two functions which can handle any of the types:
ord(c)
always gives you a non-negative value – even when passed a negativechar
or negativesigned char
– andchr
takes any valueord
produces and gives back the exact samechar
.In practice, I would probably just cast through
unsigned char
instead of using these, but they do succinctly wrap the cast, provide a convenient place to add error checking forint
-to-char
, and would be shorter and more clear when you need to use them several times in close proximity.