#include "itoa.h" /* * The heapless_itoa function is copied from the shellmatta project. */ /** * @brief itoa like function to convert int to an ascii string * @warning you have to provide a large enough buffer * @param[in] value * @param[in,out] buffer * @param[in] base * @return number of bytes in string */ uint32_t heapless_itoa(int32_t value, char *buffer, uint32_t base) { char tempBuffer[34u]; uint32_t i; uint32_t bufferIdx = 0u; int8_t digitValue; /** -# check the base for plausibility */ if((base >= 2) && (base <= 16)) { /** -# check for sign */ if(value < 0) { value = value * (-1); buffer[0u] = '-'; bufferIdx += 1u; } /** -# loop through all digits in reverse order */ i = 0u; do { digitValue = (int8_t) (value % base); tempBuffer[i] = (digitValue < 10) ? ('0' + digitValue) : ('A' + (digitValue - 10)); value /= base; i ++; }while(value > 0); /** -# store the string in the correct order onto the buffer */ while(i > 0u) { buffer[bufferIdx] = tempBuffer[i - 1u]; i --; bufferIdx ++; } } return bufferIdx; } static char num_to_hex_digit(uint8_t num, bool capitalized) { char ret = 'U'; switch (num) { case 0 ... 9: ret = '0' + num; break; case 0xA ... 0xF: if (capitalized) ret = 'A' + num - 0xA; else ret = 'a' + num - 0xA; break; default: break; } return ret; } uint32_t bytes_to_hex_string(uint8_t *input, uint32_t count, char *output_buffer, bool capitalized) { uint32_t idx; uint8_t b; for (idx = 0; idx < count; idx++) { b = input[idx]; output_buffer[2*idx] = num_to_hex_digit(b >> 4, capitalized); output_buffer[2*idx+1] = num_to_hex_digit(b & 0xF, capitalized); } return 0; }