字符串到十进制的转换 [英] conversion of string to decimal
问题描述
我正在使用RS232串行通信来旋转电机。串行通信以字符串形式完成,但我需要十进制值。任何线索如何进行。我使用c语言进行编码。我尝试使用atoi函数,但无法正常工作。
char * p ;
int z = atoi(p);
通常给出一个字符串:
char * myStr = 123;
获取int值的方式是:
int value = atoi(myStr);
一些重要的注意事项:
以下内容是必要的:
#include< stdlib.h>
,您必须确保您的字符串是一个以null结尾的字符串,否则atoi会使您的程序崩溃。 / p>
您没有给我们太多信息,但是如果您正在对微控制器进行编程(我怀疑是因为您告诉我们有关电动机的信息),那么您可能不想使用stdlib。在这种情况下,您可能使用了Costum函数。
请查看下面的代码:
int stringToInt(char * nrStr){
int nrChars = 0;
while(nrStr [nrChars]!=‘\0’){
nrChars ++;
}
int结果= 0;
int i = 0;
while(nrStr [i]!='\0'){//而你没有到达字符串的末尾
int digit = nrStr [i] -48; // 48为零ASCII代码
int exp = nrChars-i-1;
int add = digit * power(10,exp);
结果+ =添加;
i ++;
}
返回结果;
}
int power(int base,int exp){
int ret = 1;
int i;
for(i = 0; i ret * = base;
}
回返;
}
这不使用任何库函数并且可以完成此工作。我在3分钟内完成了操作,它可能会有一些小错误,它不是非常有效,并且无法验证可能的错误,但是原则上,如果您将strinToint函数传递为格式良好的整数作为以null结尾的字符串,它将输出正确的值。
如果您使用的是确实具有幂函数实现的库,请使用它代替我给您的函数,因为它根本没有效率
最后一点:如果出于某种原因需要以八进制为基础使用它,则必须碰到这行:
int add = digit * power(10,exp);
至:
int add = digit * power(8,exp);
对于十六进制,这将不起作用,并且此功能的实现将有很大的不同。
I am using RS232 serial communication to rotate the motor. The serial communication is done in strings but i need decimal value for it. Any clues how to proceed.I am coding in c language.i tried using atoi function but its not working.
char *p;
int z=atoi(p);
Usually given a string:
char * myStr= "123";
the way to obtain it's value as an int is:
int value=atoi(myStr);
Some things important to notice:
the following include is necessary:
#include <stdlib.h>
and you must be sure that your string is a null terminated string otherwise atoi will crash you program.
You didn't gave us much information but if you're programming a microcontroller (I suspect that since you told us about a motor) you maybe won't want to use stdlib. In that case you might have use a costum function.
Please take a look at the code bellow:
int stringToInt(char* nrStr){
int nrChars=0;
while(nrStr[nrChars]!='\0'){
nrChars++;
}
int result=0;
int i=0;
while(nrStr[i]!='\0'){//while you dont get to the end of the string
int digit=nrStr[i]-48;//48 is zero ascii code
int exp=nrChars-i-1;
int add=digit*power(10,exp);
result+=add;
i++;
}
return result;
}
int power(int base, int exp){
int ret=1;
int i;
for(i=0;i<exp;i++){
ret*=base;
}
return ret;
}
This does not use any library functions and does the job. I've done it in 3 minutes and it may have some small error, it's not very efficient and does not verify possible errors, but in principle if you pass the strinToint function a well formed integer as a null terminated string it will output the correct value.
If you're using a library that does have some implementation of a power function do use it instead of the one I gave you since it is not efficient at all.
One last note: if you for some reason need to use it in other basis lets say octal basis, you have to chance the line:
int add=digit*power(10,exp);
to:
int add=digit*power(8,exp);
for hexadecimal this will not work, and implementation of such a function will be significantly different.
这篇关于字符串到十进制的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!