如何将整数转换为字符串可移植? [英] How to convert an integer to a string portably?

查看:130
本文介绍了如何将整数转换为字符串可移植?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方式来一个整数转换为字符串在便携方式(便携式至少是Windows&安培之间,Linux和x86和x86_64的),我虽然 itoa(X)是标准的,就像的atoi(1)

I was looking for a way to convert an integer to a string in a portable manner (portable among at least Windows & Linux and x86 and x86_64) and I though itoa(X) to be standard just like atoi(1).

但我读了维基百科的条目:

But I read the following in the Wikipedia entry:

该itoa函数是一个wides $ P $垫非标准扩展标准C编程语言。它不能被移植地使用,因为它不以任何的C语言的标准所定义;然而,编译器通常通过头提供它,而在不合格的模式中,因为它是一个逻辑对应的标准库函数atoi

The itoa function is a widespread non-standard extension to the standard C programming language. It cannot be portably used, as it is not defined in any of the C language standards; however, compilers often provide it through the header while in non-conforming mode, because it is a logical counterpart to the standard library function atoi.

所以我想知道是否有办法做到这一点可移植的方式与否。如果我写我自己的功能,我有哪些东西要小心?

So I'd like to know if there is any way to do it in a portable manner or not. In case I have to write my own function, which things do I have to be careful with?

推荐答案

大多数情况下你只用printf(%D);

Most often you just use printf("%d");

http://en.wikipedia.org/wiki/Printf

如果您需要在缓冲区中,您可以用sprintf,但你经常转换为字符串,而不是将其写入到文件或输出设备?

You can use sprintf if you need it in a buffer, but how often do you convert to a string and not write it to a file or output device?

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

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