在C,无sprintf的字符串转换整数 [英] Converting integer to string in C without sprintf

查看:135
本文介绍了在C,无sprintf的字符串转换整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能为整数转换为字符串 C 的sprintf

推荐答案

您可以使用的 itoa 的(如有)。如果不是您的平台上,下面的实现可能感兴趣的:

You can use itoa where available. If it is not available on your platform, the following implementation may be of interest:

http://www.student.cs.uwaterloo.ca/~cs350/common/os161-src-html/atoi_8c-source.html

用法:

char *numberAsString = itoa(integerValue); 

更新

..的评论,它可能是值得修改现有itoa执行从呼叫者接收一个结果缓冲器,而不是具有itoa分配和返回缓冲器

Based on the R..'s comments, it may be worth modifying an existing itoa implementation to accept a result buffer from the caller, rather than having itoa allocate and return a buffer.

这样的实现应该同时接受一个缓冲区和缓冲区的长度,注意不要写过去呼叫者提供的缓冲区的末尾

Such an implementation should accept both a buffer and the length of the buffer, taking care not to write past the end of the caller-provided buffer.

这篇关于在C,无sprintf的字符串转换整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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