Skip to content

Commit c2e0383

Browse files
committed
Fix the conversion process from an integer to a hexadecimal string
The original implementation of __str_base16(), which converts integers to hexadecimal strings, causes errors when processing negative integers. It produces redundant 'f' characters at the beginning of the converted string due to sign extension and results in unexpected output when calling printf() or sprintf(). Therefore, this commit fixes it and ensure correct conversion.
1 parent 861d7d2 commit c2e0383

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

lib/c.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ void __str_base8(char *pb, int val)
178178
void __str_base16(char *pb, int val)
179179
{
180180
int c = INT_BUF_LEN - 1;
181-
while (c > 0) {
181+
int times = sizeof(int) << 1;
182+
for (int i = 0; i < times; i++) {
182183
int v = val & 0xf;
183184
if (v < 10)
184185
pb[c] = '0' + v;

0 commit comments

Comments
 (0)