Split uc_human_bytesz in uc_human_bytesz_dec/uc_human_bytesz_bin, for

decimal and binary conversion (http://en.wikipedia.org/wiki/Megabyte)
This commit is contained in:
Nils O. Selåsdal
2013-11-19 11:33:43 +01:00
parent ac95b1002c
commit 8083fa54cf
3 changed files with 61 additions and 10 deletions
+20 -1
View File
@@ -3,7 +3,7 @@
#include "ucore/string.h"
char *
uc_human_bytesz(uint64_t bytes, char *result, size_t result_len)
uc_human_bytesz_dec(uint64_t bytes, char *result, size_t result_len)
{
if (bytes < 1000) {
snprintf(result, result_len, "%" PRIu64 " b", bytes);
@@ -22,3 +22,22 @@ uc_human_bytesz(uint64_t bytes, char *result, size_t result_len)
return result;
}
char *
uc_human_bytesz_bin(uint64_t bytes, char *result, size_t result_len)
{
if (bytes < 1024) {
snprintf(result, result_len, "%" PRIu64 " b", bytes);
} else if (bytes < 1048576) {
snprintf(result, result_len, "%.2f KiB", bytes/1024.0);
} else if (bytes < 1073741824ULL) {
snprintf(result, result_len, "%.2f MiB", bytes/1048576.0);
} else if (bytes < 1099511627776ULL) {
snprintf(result, result_len, "%.2f GiB", bytes/1073741824.0);
} else if (bytes < 1125899906842624ULL) {
snprintf(result, result_len, "%.2f TiB", bytes/1099511627776.0);
} else {
snprintf(result, result_len, "%.2f PiB", bytes/1125899906842624.0);
}
return result;
}