4 Digits in output were reversed (e.g. 192 was written as "291").
10 #pragma disable_warning 85
13 #define UCHAR unsigned char
14 #define USHORT unsigned short
15 #define PCHAR unsigned char *
17 UCHAR
mod_16x8(USHORT s16
, UCHAR i8
)
22 USHORT
div_16x8(USHORT s16
, UCHAR i8
)
28 void itoa(USHORT sVal
, PCHAR pBuf
, UCHAR iRadix
)
30 PCHAR p
; // pointer to traverse string
31 PCHAR pFirstDigit
; // pointer to first digit
32 UCHAR iTmp
; // temp UCHAR
35 pFirstDigit
= p
; // save pointer to first digit
39 iTmp
= mod_16x8(sVal
, iRadix
);
40 sVal
= div_16x8(sVal
, iRadix
);
42 // convert to ascii and store
43 *p
++ = (iTmp
> 9) ? iTmp
- 10 + 'A' : iTmp
+ '0'; // a letter or a digit
46 // We now have the digit of the number in the buffer, but in reverse order. Thus we reverse them now.
48 *p
-- = '\0'; // terminate string; p points to last digit
54 *pFirstDigit
= iTmp
; // swap *p and *pFirstDigit
56 ++pFirstDigit
; // advance to next two digits
57 } while (pFirstDigit
< p
); // repeat until halfway