哪里是在Linux中itoa功能? [英] Where is the itoa function in Linux?

查看:107
本文介绍了哪里是在Linux中itoa功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

itoa()是一个非常方便的功能,将数字转换为字符串。 Linux的似乎并不具有 itoa(),是否有相同功能或做我必须使用的sprintf(STR,%D,NUM )


解决方案

编辑:对不起,我应该记住的是,这台机器是决然不规范,已经插上各类非标的libc 学术目的的实现; - )

由于 itoa()确实是非标准的,由几个有用的评论者提到的,最好使用的sprintf(target_string,% D,source_int)或(更好的,因为它是安全的缓冲区溢出)的snprintf(target_string,size_of_target_string_in_bytes,%D,source_int) 。我知道这是不是很简明或酷为 itoa(),但至少你可以写一次,到处运行(TM); - )

这里是老(编辑)答案

您是在陈述正确的,默认的 GCC的libc 不包括 itoa(),像其他几个平台时,由于它不是技术上是标准的一部分。请参见这里多一点信息。请注意,您必须

 的#include<&stdlib.h中GT;

当然,你已经知道这一点,因为你想的使用 itoa()在Linux上后,presumably使用它在另一个平台...但是... code(从上面的链接偷来的)看起来像:

示例

  / * itoa例如* /
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
  INT I;
  炭缓冲液[33];
  的printf(请输入一个数字:);
  scanf函数(%d个,&安培; I)
  itoa(ⅰ,缓冲液,10);
  的printf(十进制:%S \\ n,缓冲区);
  itoa(ⅰ,缓冲液,16);
  的printf(十六进制:%S \\ n,缓冲区);
  itoa(ⅰ,缓冲液,2);
  的printf(二进制文件:%S​​ \\ n,缓冲区);
  返回0;
}

输出:


 输入一个数字:1750
十进制:1750
十六进制:6d6
二:11011010110


希望这有助于!

itoa() is a really handy function to convert a number to a string. Linux does not seem to have itoa(), is there an equivalent function or do I have to use sprintf(str, "%d", num)?

解决方案

EDIT: Sorry, I should have remembered that this machine is decidedly non-standard, having plugged in various non-standard libc implementations for academic purposes ;-)

As itoa() is indeed non-standard, as mentioned by several helpful commenters, it is best to use sprintf(target_string,"%d",source_int) or (better yet, because it's safe from buffer overflows) snprintf(target_string, size_of_target_string_in_bytes, "%d", source_int). I know it's not quite as concise or cool as itoa(), but at least you can Write Once, Run Everywhere (tm) ;-)

Here's the old (edited) answer

You are correct in stating that the default gcc libc does not include itoa(), like several other platforms, due to it not technically being a part of the standard. See here for a little more info. Note that you have to

#include <stdlib.h>

Of course you already know this, because you wanted to use itoa() on Linux after presumably using it on another platform, but... the code (stolen from the link above) would look like:

Example

/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char buffer [33];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);
  printf ("binary: %s\n",buffer);
  return 0;
}

Output:

Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110

Hope this helps!

这篇关于哪里是在Linux中itoa功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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