Char *到int [英] Char * to int

查看:48
本文介绍了Char *到int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将char *转换为整数?

如果是,怎么做?

解决方案

Kay写道:

是否可以将char *转换为整数?
如果是,该怎么做?




char const * z =" 12";


stringstream stream = z;

int q = 0;


if(z>> q)

cout<< "是" <<结束;


(strtol()也有效。)


-

Phlip
< a rel =nofollowhref =http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces\"target =_ blank> http://industrialxp.org/community/bi...UserInterfaces


" Kay" < ER ********* @ yahoo.com.hk>在留言中写道

news:41 ************** @ yahoo.com.hk ...

是否可以转换char *到整数?


不,不便携。

如果是,怎么办?




也许你想将一个数字的文本表示转换为整数类型。如果是这样,请使用字符串流。


#include< iostream>

#include< sstream>


int main()

{

const char * x =" 123";

std :: istringstream iss(x);

int i(0);


if(iss>> i)

std :: cout<< i<< ''\ n'';

其他

std :: cerr<< 无法转换\ n;


返回0;

}

-Mike


"凯" < ER ********* @ yahoo.com.hk>在留言中写道

news:41 ************** @ yahoo.com.hk ...

是否可以转换char *到整数?
如果是,怎么做?




将std :: strings转换为数字的FAQ中列出的技术将是

也适用于C风格的字符串。请参阅常见问题

http:// www.parashift.com/c++-faq-lite/) ,第38条(其他

技术问题),问题2(我如何转换标准::字符串到

号码?")。


-

David Hilsee


Is it possible to convert char * to integer ?
if yes, how to do it ?

解决方案

Kay wrote:

Is it possible to convert char * to integer ?
if yes, how to do it ?



char const * z = "12";

stringstream stream = z;
int q = 0;

if (z >> q)
cout << "yes" << endl;

(strtol() also works.)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


"Kay" <er*********@yahoo.com.hk> wrote in message
news:41**************@yahoo.com.hk...

Is it possible to convert char * to integer ?
No, not portably.
if yes, how to do it ?



Perhaps you want to convert the textual representation
of a number to an integer type. If so, use a stringstream.

#include <iostream>
#include <sstream>

int main()
{
const char *x = "123";
std::istringstream iss(x);
int i(0);

if(iss >> i)
std::cout << i << ''\n'';
else
std::cerr << "unable to convert\n";

return 0;
}
-Mike


"Kay" <er*********@yahoo.com.hk> wrote in message
news:41**************@yahoo.com.hk...

Is it possible to convert char * to integer ?
if yes, how to do it ?



The technique listed in the FAQ for converting std::strings to numbers will
also work for C-style strings. See the FAQ
(http://www.parashift.com/c++-faq-lite/), section 38 ("Miscellaneous
technical issues"), question 2 ("How do I convert a std::string to a
number?").

--
David Hilsee


这篇关于Char *到int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆