2018-08-29 19:07:52 +00:00
|
|
|
#include "samd51j18a.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "usb_util.h"
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
char digit(int d, int radix) {
|
|
|
|
if (d < 10) {
|
2018-08-29 19:07:52 +00:00
|
|
|
return d + '0';
|
2019-08-30 18:19:03 +00:00
|
|
|
} else {
|
2018-08-29 19:07:52 +00:00
|
|
|
return d - 10 + 'A';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
int UTIL_ltoa_radix(int64_t value, char *dest, int radix) {
|
2022-02-12 18:29:31 +00:00
|
|
|
int64_t original = value; // save original value
|
2019-08-30 18:19:03 +00:00
|
|
|
char buf[25] = "";
|
|
|
|
int c = sizeof(buf) - 1;
|
|
|
|
int last = c;
|
|
|
|
int d;
|
|
|
|
int size;
|
2018-08-29 19:07:52 +00:00
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
if (value < 0) // if it's negative, take the absolute value
|
2018-08-29 19:07:52 +00:00
|
|
|
value = -value;
|
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
do // write least significant digit of value that's left
|
2018-08-29 19:07:52 +00:00
|
|
|
{
|
2019-08-30 18:19:03 +00:00
|
|
|
d = (value % radix);
|
2018-08-29 19:07:52 +00:00
|
|
|
buf[--c] = digit(d, radix);
|
|
|
|
value /= radix;
|
|
|
|
} while (value);
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
if (original < 0) buf[--c] = '-';
|
2018-08-29 19:07:52 +00:00
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
size = last - c + 1; // includes null at end
|
2018-08-29 19:07:52 +00:00
|
|
|
memcpy(dest, &buf[c], last - c + 1);
|
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
return (size - 1); // without null termination
|
2018-08-29 19:07:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
int UTIL_ltoa(int64_t value, char *dest) {
|
|
|
|
return UTIL_ltoa_radix(value, dest, 10);
|
|
|
|
}
|
2018-08-29 19:07:52 +00:00
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
int UTIL_itoa(int value, char *dest) {
|
|
|
|
return UTIL_ltoa_radix((int64_t)value, dest, 10);
|
|
|
|
}
|
2018-08-29 19:07:52 +00:00
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
int UTIL_utoa(uint32_t value, char *dest) {
|
|
|
|
return UTIL_ltoa_radix((int64_t)value, dest, 10);
|
|
|
|
}
|