在c ++中将字符串转换为整数 [英] convert string to integer in c++

查看:143
本文介绍了在c ++中将字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我知道它被问过很多次,但我没有找到我的具体问题的答案。

Hello I know it was asked many times but I hadn't found answer to my specific question.

我想转换只包含字符串只有十进制数:

I want to convert only string that contains only decimal numbers:

例如256可以,但256a不是。

For example 256 is OK but 256a is not.

字符串?

感谢

推荐答案

检查可选的,我可以想到的是这样:

The simplest way that makes error checking optional that I can think of is this:

char *endptr;
int x = strtol(str, &endptr, 0);
int error = (*endptr != '\0');

这篇关于在c ++中将字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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