Skip to content

Instantly share code, notes, and snippets.

@idolpx
Last active July 25, 2022 22:51
Show Gist options
  • Save idolpx/735694e2b35e41556987d37eb5078528 to your computer and use it in GitHub Desktop.
Save idolpx/735694e2b35e41556987d37eb5078528 to your computer and use it in GitHub Desktop.
std::string byteSuffixes[9] = { "", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
std::string formatBytes(uint64_t value) {
uint8_t i = 0;
double n = 0;
char *f = NULL;
do {
i++;
n = value / std::pow(1024, ++i);
} while ( n >= 1 );
n = value / std::pow(1024, --i);
asprintf(&f, "%.2f %s", n, byteSuffixes[i].c_str());
return f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment