本帖最后由 弘毅 于 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[INTERMEDIATE_MAX_WIDTH + 1];
-
- 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[i] = leader_character;
- ++i;
- }
- }
- n = i + intermediate_len;
- int_fast8_t j = 0;
- while (i <= n)
- {
- numeric_string[i] = intermediate_string[j];
- ++i;
- ++j;
- }
- i -= 1;
- return i; //Returns the numeric string length
- }
复制代码
==========================
纯C11草案标准写的,并兼容Arduino
有什么使用问题欢迎留言 |