First, don't use malloc() for a small, fixed amount of space; use an array. Second, don't switch, and in general, in kernel code, try to avoid diverging control paths. Finally, if it's supposed to be a C-style string, it needs to end with '\0'
.
So consider something like:
#include <cstdint>
enum { max_digits = 3, modulus = 10 };
struct stringized_byte_t {
char[max_digits+1] buffer;
}
stringized_byte_t stringize_a_byte(uint8_t my_byte)
{
uint8_t digits[max_digits];
uint8_t num_digits = 1;
uint8_t remainder = my_byte;
while(remainder >= modulus) {
uint8_t dividend = remainder / modulus;
digits[num_digits - 1] = remainder - dividend * modulus;
num_digits++;
remainder = dividend;
}
// at this point we have one digit left (although it might be 0),
// and we know the overall number of digits, so:
digits[num_digits - 1] = remainder;
// Now we need to flip the digit direction to fit the printing order,
// and terminate the string
stringized_byte_t sb;
for(int i = 0; i < num_digits; i++) {
sb.buffer[i] = '0' + digits[num_digits - i - 1];
}
sb.buffer[num_digits] = '\0';
return sb;
}
Note I used C-style coding rather than "pimping up" the class, so you can very easily convert this code into proper C.