Linux 想在没有 itoa 函数的情况下将整数转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9994742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Want to Convert Integer to String without itoa function
提问by user1089679
i want to convert int to char
* in C without using itoa()
function.
我想int to char
在不使用itoa()
函数的情况下在 C 中转换* 。
Because on my Linux Systems i itoa
function is not there. i am using this code which i found from here
因为在我的 Linux 系统上,我的itoa
功能不存在。我正在使用我从这里找到的这段代码
I want to run this function on Embedded devices also which are using Linux.
我想在也使用 Linux 的嵌入式设备上运行此功能。
So i am looking for without use of itoa
.
所以我正在寻找不使用itoa
.
I dnt want to use sprintf
also because its uses for just prints.
我sprintf
也不想使用,因为它仅用于打印。
So any body please help me to figured out this problem.
所以任何机构请帮助我解决这个问题。
Thanks
谢谢
采纳答案by user1089679
I found solution regarding this..
我找到了关于这个的解决方案..
I am Happy to and i want which i expected.
我很高兴,我想要我所期望的。
#include <string.h>
#include <stdlib.h>
char *i_to_a(int num);
int main()
{
char *str = i_to_a(4567);
printf("%s",str);
free(str);
str = NULL;
return 0;
}
int no_of_digits(int num)
{
int digit_count = 0;
while(num > 0)
{
digit_count++;
num /= 10;
}
return digit_count;
}
char *i_to_a(int num)
{
char *str;
int digit_count = 0;
if(num < 0)
{
num = -1*num;
digit_count++;
}
digit_count += no_of_digits(num);
str = malloc(sizeof(char)*(digit_count+1));
str[digit_count] = 'char str[LEN];
snprintf(str, sizeof(str), "%d", num);
';
while(num > 0)
{
str[digit_count-1] = num%10 + '0';
num = num/10;
digit_count--;
}
if(digit_count == 1)
str[0] = '-';
return str;
}
回答by cnicutar
Thing is snprintf
is the perfect function for this:
Thing issnprintf
是一个完美的功能:
#include <stdio.h>
#include <string.h>
#if 0
char *strrev(char *str){
char c, *front, *back;
if(!str || !*str)
return str;
for(front=str,back=str+strlen(str)-1;front < back;front++,back--){
c=*front;*front=*back;*back=c;
}
return str;
}
#endif
char *itoa(int v, char *buff, int radix_base){
static char table[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char *p=buff;
unsigned int n = (v < 0 && radix_base == 10)? -v : (unsigned int) v;
while(n>=radix_base){
*p++=table[n%radix_base];
n/=radix_base;
}
*p++=table[n];
if(v < 0 && radix_base == 10) *p++='-';
*p='void getDecStr (uint8_t* str, uint8_t len, uint32_t val)
{
uint8_t i;
for(i=1; i<=len; i++)
{
str[len-i] = (uint8_t) ((val % 10UL) + '0');
val/=10;
}
str[i-1] = '##代码##';
}
';
return strrev(buff);
}
int main ()
{
int i;
char str[33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,str,10);
printf ("decimal: %s\n", str);
itoa (i, str, 16);
printf ("hexadecimal: %s\n", str);
itoa (i, str, 2);
printf ("binary: %s\n", str);
return 0;
}
回答by BLUEPIXY
回答by Lundin
Here is a simple snippet you can use. There are more elegant and advanced ways, but this gets the job done.
这是您可以使用的简单片段。有更优雅和高级的方法,但这可以完成工作。
In embedded projects in the past, I have measured this to be approximately 1000 times more efficient than sprintf(). This code is also MISRA-C compliant.
在过去的嵌入式项目中,我测得这比 sprintf() 的效率高出大约 1000 倍。此代码也符合 MISRA-C。
##代码##