贡献一个开源的整型数转字符串函数
本帖最后由 弘毅 于 2017-11-3 12:24 编辑/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
uint_fast8_t integer_number_to_string (int32_t input_number, char leader_character, uint_fast8_t display_width, char numeric_string[], uint_fast8_t array_len)//input_number range is INT32_MAX to INT32_MIN. Returns the numeric string length.
{
const uint_fast8_t INTERMEDIATE_MAX_WIDTH = 1 + 10;//sign + Max significant figures, Max significant figures = int32_t floor(log10(-INT32_MIN)) + 1
char intermediate_string;
if (leader_character < '\x0')
{
return 0;
}
if (display_width >= array_len)
{
return 0;
}
if (array_len <= 1)
{
return 0;
}
#if defined(__cplusplus)
int_fast8_t intermediate_len = sprintf(intermediate_string, "%-""li", input_number);
#else
int_fast8_t intermediate_len = sprintf(intermediate_string, "%-"PRIi32, input_number);
#endif
if (intermediate_len > display_width)
{
return 0;
}
int_fast8_t i = 0;
int_fast8_t n = display_width - (intermediate_len + 1);
if (isgraph(leader_character))
{
while (i <= n)
{
numeric_string = leader_character;
++i;
}
}
n = i + intermediate_len;
int_fast8_t j = 0;
while (i <= n)
{
numeric_string = intermediate_string;
++i;
++j;
}
i -= 1;
return i;//Returns the numeric string length
}
==========================
纯C11草案标准写的,并兼容Arduino
有什么使用问题欢迎留言 为更方便大家应用,变更了开源许可证
页:
[1]