Foo*_*Bah 481
取决于你想做什么:
要将值读作ascii代码,您可以编写
char a = 'a';
int ia = (int)a;
/* note that the int cast is not necessary -- int ia = a would suffice */
Run Code Online (Sandbox Code Playgroud)
该字符转换'0' -> 0,'1' -> 1等等,你可以写
char a = '4';
int ia = a - '0';
/* check here if ia is bounded by 0 and 9 */
Run Code Online (Sandbox Code Playgroud)
小智 79
那么,在ASCII码中,数字(数字)从48开始.你需要做的就是:
int x = (int)character - 48;
Run Code Online (Sandbox Code Playgroud)
Mat*_*ner 58
C和C++总是至少提升类型int.此外,字符文字int在C和charC++中都是类型.
您char只需指定一个类型即可转换类型int.
char c = 'a'; // narrowing on C
int a = c;
Run Code Online (Sandbox Code Playgroud)
Lun*_*din 29
char只是一个1字节的整数.char类型没有什么神奇之处!正如您可以为int指定short,或者为long指定int,您可以为int指定char.
是的,原始数据类型的名称恰好是"char",这暗示它应该只包含字符.但实际上,"char"只是一个糟糕的名字选择,可以混淆每个试图学习语言的人.更好的名称是int8_t,如果编译器遵循最新的C标准,则可以使用该名称.
当然,在进行字符串处理时,您应该使用char类型,因为经典ASCII表的索引适合1个字节.然而,您也可以使用常规的整数进行字符串处理,尽管在现实世界中没有任何实际原因可以帮助您实现这一目标.例如,以下代码将完美地运行:
int str[] = {'h', 'e', 'l', 'l', 'o', '\0' };
for(i=0; i<6; i++)
{
printf("%c", str[i]);
}
Run Code Online (Sandbox Code Playgroud)
您必须意识到字符和字符串只是数字,就像计算机中的其他所有内容一样.当您在源代码中写入'a'时,它会被预处理为数字97,这是一个整数常量.
所以如果你写一个像这样的表达式
char ch = '5';
ch = ch - '0';
Run Code Online (Sandbox Code Playgroud)
这实际上相当于
char ch = (int)53;
ch = ch - (int)48;
Run Code Online (Sandbox Code Playgroud)
然后进行C语言整数提升
ch = (int)ch - (int)48;
Run Code Online (Sandbox Code Playgroud)
然后截断为char以适合结果类型
ch = (char)( (int)ch - (int)48 );
Run Code Online (Sandbox Code Playgroud)
在这些行之间有很多微妙的事情,其中char被隐含地视为int.
Fre*_*urk 16
(这个答案解决了C++方面的问题,但C中也存在符号扩展问题.)
处理所有三种char类型(signed,unsigned和char)比首次出现时更精细.范围0到SCHAR_MAX(对于8位为127 char)的值很容易:
char c = somevalue;
signed char sc = c;
unsigned char uc = c;
int n = c;
Run Code Online (Sandbox Code Playgroud)
但是,当somevalue超出该范围时,只有通过unsigned char才能为char所有三种类型中的"相同" 值提供一致的结果:
char c = somevalue;
signed char sc = c;
unsigned char uc = c;
// Might not be true: int(c) == int(sc) and int(c) == int(uc).
int nc = (unsigned char)c;
int nsc = (unsigned char)sc;
int nuc = (unsigned char)uc;
// Always true: nc == nsc and nc == nuc.
Run Code Online (Sandbox Code Playgroud)
这在使用ctype.h中的函数时非常重要,例如isupperor toupper,因为符号扩展:
char c = negative_char; // Assuming CHAR_MIN < 0.
int n = c;
bool b = isupper(n); // Undefined behavior.
Run Code Online (Sandbox Code Playgroud)
注意通过int的转换是隐式的; 这有相同的UB:
char c = negative_char;
bool b = isupper(c);
Run Code Online (Sandbox Code Playgroud)
要解决这个问题,请通过safe_ctypeunsigned char包装ctype.h函数来轻松完成:
template<int (&F)(int)>
int safe_ctype(unsigned char c) { return F(c); }
//...
char c = CHAR_MIN;
bool b = safe_ctype<isupper>(c); // No UB.
std::string s = "value that may contain negative chars; e.g. user input";
std::transform(s.begin(), s.end(), s.begin(), &safe_ctype<toupper>);
// Must wrap toupper to eliminate UB in this case, you can't cast
// to unsigned char because the function is called inside transform.
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为任何采用三种char类型中的任何一种的函数也可以采用其他两种char类型.它导致两个函数可以处理任何类型:
int ord(char c) { return (unsigned char)c; }
char chr(int n) {
assert(0 <= n); // Or other error-/sanity-checking.
assert(n <= UCHAR_MAX);
return (unsigned char)n;
}
// Ord and chr are named to match similar functions in other languages
// and libraries.
Run Code Online (Sandbox Code Playgroud)
ord(c)总是给你一个非负值 - 即使传递一个负数char或负数signed char- 并chr取任何值ord产生并给出完全相同的值char.
在实践中,我可能只是通过投unsigned char,而不是用这些,但他们简洁包裹投,提供了方便的地方加入错误检查int至- char,并会更短,更清晰,当你需要使用它们几次在附近.
her*_*tao 11
用途static_cast<int>:
int num = static_cast<int>(letter); // if letter='a', num=97
Run Code Online (Sandbox Code Playgroud)
编辑:你可能应该尽量避免使用(int)
int num =(int)letter;
检查为什么使用static_cast <int>(x)而不是(int)x?了解更多信息.
我对nullC 有绝对的技能,但需要进行简单的分析:
char* something = "123456";
int number = parseInt(something);
Run Code Online (Sandbox Code Playgroud)
...这对我有用:
int parseInt(char* chars)
{
int sum = 0;
int len = strlen(chars);
for (int x = 0; x < len; x++)
{
int n = chars[len - (x + 1)] - '0';
sum = sum + powInt(n, x);
}
return sum;
}
int powInt(int x, int y)
{
for (int i = 0; i < y; i++)
{
x *= 10;
}
return x;
}
Run Code Online (Sandbox Code Playgroud)