I need to figure out how to make a routine for the LR35902 (the Game Boy's Z80-like CPU) that will take the contents of HL (not where it points to, the actual number stored in it) and convert it into 1 to 5 (actual values would range from 0 to 65,535, since HL is 16-bit) non-ASCII* characters using the decimal system. I have routines to print and can easily figure out a way to crop leading zeroes.
All I need is at least some pseudo-code for this function and at most an actual RGBDS LR35902 ASM routine.
*Instead of ASCII, I use my own 1-byte-per-character charmap, in which the character 0 is hexadecimal byte $17, increasing until 9, which is $20.