将命令行参数转换为整数 [英] Converting command line arguments to integers

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

问题描述

我需要通过argc *argv[]从命令行读取两个整数,以进行作业.我怎样才能做到这一点?如您所知,参数是字符串,为了使用它,我必须将它们转换为整数.谢谢.

I need to read two integers from the command line via argc and *argv[] for my homework. How can I achieve this? As you know, the parameters are strings and I have to convert them to integers in order to use it. Thank you.

推荐答案

功能 atoi [< ^ ]会将字符串转换为整数.
function atoi[^] will convert string to integer.


atoi在这种情况下是完美的,但是将来还有其他功能可能对类似任务有用

strtol-长字符串"
strtoul-字符串到无符号长整数"
strtod-字符串加倍"

这些函数采用char **_EndPtr参数,该参数返回时将保持新字符串指针在处理数字后的位置.如果字符串中有数字,这将很有帮助.

strtolstrtoul将基数作为附加参数来指定字符串的数字基数,因此您可以将二进制,八进制,十进制,十六进制或任何其他基本字符串转换为其等效的整数

在MSDN上查看他们的页面: atoi [ ^ ], strtol [ stroul [ strtod [ ^ ]
atoi is perfect in this case, however there are other functions which you may find usefull in the future for similar tasks

strtol - "string to long"
strtoul - "string to unsigned long"
strtod - "string to double"

These functions take a char **_EndPtr parameter which on return holds the position that the new string pointer after the number processed. This is helpful if you have a number in a string.

strtol and strtoul take the radix as an additional parameter to specify the number base of the string so you can convert binary, octal, decimal, hexadecimal or any other base string to its integer equivalent

check out their pages on MSDN: atoi[^], strtol[^], stroul[^], strtod[^]


这篇关于将命令行参数转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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