【发布时间】:2011-08-30 21:46:59
【问题描述】:
有人知道如何将 char 数组转换为单个 int 吗?
char hello[5];
hello = "12345";
int myNumber = convert_char_to_int(hello);
Printf("My number is: %d", myNumber);
【问题讨论】:
-
该代码是否按原样编译?不应该。
-
不应该这样。 convert_char_to_int(hello) 不是一个实际的函数。我在问我应该用什么函数/方法来代替我的理论:“convert_char_to_int(hello)”?
-
hello是一个不可修改的 lvalue 所以hello = "12345";甚至不会编译。 -
好吧,这个:codepad.org/oSgK5nK4我如何转换它?
-
您是否完全坚持使用 char 数组和 printf 等 C 风格的结构?如果没有,请查看
std::string的字符,查看 C++ iostreams 以打印输出 (std::cout),查看boost::lexical_cast进行转换(正如已经指出的那样)。