将char数组转换为单个int? [英] Convert char array to single int?
本文介绍了将char数组转换为单个int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人知道如何将一个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:使用
}
< lexical_cast
(最合适且最简单) 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 functionality
int 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++ Streams
std::string hello("123");
std::stringstream str(hello);
int x;
str >> x;
if (!str)
{
// The conversion failed.
}
这篇关于将char数组转换为单个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文