24 bool format_i64(
char *dst,
size_t dst_len,
const int64_t value)
26 char temp[] =
"-9223372036854775808";
37 *ptr++ =
'0' + (num % 10) * sign;
44 else if (value == 0) {
48 int distance = (ptr - temp) + 1;
50 if ((
int) dst_len < distance) {
56 while (--ptr >= temp) {
75 out[i] = in % 10 +
'0';
103 char buffer[21] = {0};
105 if (!
format_u64(buffer,
sizeof(buffer), value)) {
109 size_t digits = strlen(buffer);
111 if (digits <= decimals) {
112 if (dst_len <= 2 + decimals - digits) {
117 for (
uint16_t i = 0; i < decimals - digits; i++, dst++) {
120 dst_len -= 2 + decimals - digits;
121 strncpy(dst, buffer, dst_len);
124 if (dst_len <= digits + 1 + decimals) {
128 const size_t shift = digits - decimals;
129 memmove(dst, buffer, shift);
131 strncpy(dst + shift + 1, buffer + shift, decimals);
143 size_t len = strlen(dst);
145 while (len > 0 && (dst[len - 1] ==
'0' || dst[len - 1] ==
'.')) {
146 if (dst[len - 1] ==
'.') {
158 if (out_len < 2 * in_len + 1) {
162 const char hex[] =
"0123456789ABCDEF";
166 while (i < in_len && (i * 2 + (2 + 1)) <= out_len) {
167 uint8_t high_nibble = (in[i] & 0xF0) >> 4;
168 *out = hex[high_nibble];
171 uint8_t low_nibble = in[i] & 0x0F;
172 *out = hex[low_nibble];