要将单个char转换为int,可以使用C++中的内置函数static_cast<int>(char_value)
。这里是一个完整的示例:
#include<iostream>
int main() {
char char_value = 'A';
int int_value = static_cast<int>(char_value);
std::cout << "The ASCII value of "<< char_value << " is: "<< int_value<< std::endl;
return 0;
}
在这个示例中,我们将字符'A'转换为其对应的ASCII值(65)。这种方法适用于任何字符,包括数字、字母和特殊字符。
领取专属 10元无门槛券
手把手带您无忧上云