本文介绍了将char数组转换为单个int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 任何人知道如何将一个char数组转换为一个int? char hello [5]; hello =12345; int myNumber = convert_char_to_int(hello); Printf(我的号码是:%d,myNumber); 解决方案有很多种方法可以将字符串转换为int pre> int main() { // char hello [5]; // hello =12345; ---> This wont compile char hello [] =12345; Printf(我的号码是:%d,atoi(hello)); return 0;解决方案2:使用 } (最合适且最简单) int x = boost :: lexical_cast< int>(12345);解决方案3:使用 C ++ Streams $ std :: string hello(123); std :: stringstream str(hello); int x; str>> X; if(!str) { //转换失败。 } Anyone know how to convert a char array to a single int? char hello[5];hello = "12345";int myNumber = convert_char_to_int(hello);Printf("My number is: %d", myNumber); 解决方案 There are mulitple ways of converting a string to an int.Solution 1: Using Legacy C functionalityint main(){ //char hello[5]; //hello = "12345"; --->This wont compile char hello[] = "12345"; Printf("My number is: %d", atoi(hello)); return 0;}Solution 2: Using lexical_cast(Most Appropriate & simplest)int x = boost::lexical_cast<int>("12345");Solution 3: Using C++ Streamsstd::string hello("123");std::stringstream str(hello);int x;str >> x;if (!str){ // The conversion failed.} 这篇关于将char数组转换为单个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 02:41