字符串到整数 [英] String to integer

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

问题描述

您好,


我是C语言编程的新手,我正在寻找一种将

字符串解析为整数的方法。

我知道怎么用Java做,但是这在C中不起作用; o)

我搜索了互联网,但我还没找到它。


帮助请

谢谢

Freaker85

解决方案



朋友,


您可以使用库例程atoi(),同样的


Thxs,

ragi


Freaker85写道:


您好,


我是C语言编程的新手,我正在寻找一种将

字符串解析为整数的方法。

我知道该怎么做它在Java中,但在C中不起作用; o)

我搜索了互联网,但我还没有找到它。


请帮助

谢谢

Freaker85




ragi写道:


嗨朋友,


您可以使用库例行atoi(),同样的


Thxs,

ragi


Freaker85写道:


您好,


我是C语言编程的新手,我正在寻找一种方法来解析

字符串转换为整数。

我知道如何用Java做,但是这在C中不起作用; o)

我搜索了互联网,但我没有还没找到。


帮助请

谢谢

Freaker85



你可以使用sscanf()


Freaker85写道:


您好,


我是C语言编程的新手,我正在寻找一种将

字符串解析为整数的方法。

我知道怎么用Java做,但那并不是ork in C; o)

我搜索了互联网,但我还没有找到它。



下面显示了怎么做它。阅读系统上的文档

sscanf。

------------------------- ------------

#include< stdio.h>


int main(无效)

{

int dest = 0;

char * s1 [] = {

" 1234",/ * 1234 * /

" 0x61",/ * 97 * /

" 055",/ * 45 * /

" -55", / * -55 * /

};

int i;


for(i = 0; i< sizeof s1 / sizeof s1 [0]; i ++){

/ *来自sscanf的回报告诉你成功读入了多少

*值。自

*我们只读一个值

*(dest),我们检查我们从sscanf获得至少1的返回

*。

* /

if(sscanf(s1 [i],%i,& dest)< 1){

printf(" ;错误%i \ n",i);

}其他{

/ *我们使用%i读取,但打印在

*基数10(%d)。如果我们读入%d,那么

*我们就不能正确读取0x61或055.

* /

printf("%i \ n",dest);

}

}

返回0;

}


鹅,


Hello,

I am new at programming in C and I am searching a manner to parse a
string into an integer.
I know how to do it in Java, but that doesn''t work in C ;o)
I searched the internet but I didn''t found it yet.

help please
thank you
Freaker85

解决方案


Hi Friend,

You can use library routine atoi(), for the same

Thxs,
ragi

Freaker85 wrote:

Hello,

I am new at programming in C and I am searching a manner to parse a
string into an integer.
I know how to do it in Java, but that doesn''t work in C ;o)
I searched the internet but I didn''t found it yet.

help please
thank you
Freaker85



ragi wrote:

Hi Friend,

You can use library routine atoi(), for the same

Thxs,
ragi

Freaker85 wrote:

Hello,

I am new at programming in C and I am searching a manner to parse a
string into an integer.
I know how to do it in Java, but that doesn''t work in C ;o)
I searched the internet but I didn''t found it yet.

help please
thank you
Freaker85

you can use sscanf ()


Freaker85 wrote:

Hello,

I am new at programming in C and I am searching a manner to parse a
string into an integer.
I know how to do it in Java, but that doesn''t work in C ;o)
I searched the internet but I didn''t found it yet.

The following shows how to do it. Read the documentation
on your system for sscanf.
-------------------------------------
#include <stdio.h>

int main (void)
{
int dest = 0;
char *s1[] = {
"1234", /* 1234 */
"0x61", /* 97 */
"055", /* 45 */
"-55", /* -55 */
};
int i;

for (i=0; i<sizeof s1 / sizeof s1[0]; i++) {
/* The return from sscanf tells you how many
* values were successfully read in. Since
* we are reading in only a single value
* (dest), we check that we at get a return
* of at least 1 from sscanf.
*/
if (sscanf (s1[i], "%i", &dest)<1) {
printf ("error %i\n", i);
} else {
/* We read in using %i, but print in
* base 10 (%d). If we read in %d, then
* we would not read 0x61 or 055 correctly.
*/
printf ("%i\n", dest);
}
}
return 0;
}

goose,


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

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