1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include <lib.h>
static char suffixes[] = { 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q' };
char *btoa(size_t bytes, char *buf)
{
// no suffix if under 1K, print up to four digits
if (bytes < 1024) {
ultoa(bytes, buf, 10);
return buf;
}
// store one digit of remainder for decimal
unsigned int remainder;
// power of 1024
int power = 0;
// iterate until remaining bytes fits in three digits
while (bytes >= 1000) {
remainder = (bytes % 1024) * 10 / 1024;
bytes /= 1024;
power += 1;
}
// end of number
char *end;
if (bytes >= 10) {
// no decimal
end = ultoa(bytes, buf, 10);
} else {
// decimal
end = ultoa(bytes, buf, 10);
end[0] = '.';
end = ultoa(remainder, end + 1, 10);
}
// add suffix
end[0] = suffixes[power - 1];
end[1] = '\0';
return buf;
}
|