C++ Convert bytes to string ( Bytes, KB, MB, GB, TB )
Hello, This function serves to convert a size of a module for example, string, and shows the size, in bytes, KB, MB, GB, or TB ...
Code:
char* BytesToSize( float Bytes ) {
float tb = 1099511627776;
float gb = 1073741824;
float mb = 1048576;
float kb = 1024;
char returnSize[256];
if( Bytes >= tb )
sprintf(returnSize, "%.2f TB", (float)Bytes/tb);
else if( Bytes >= gb && Bytes < tb )
sprintf(returnSize, "%.2f GB", (float)Bytes/gb);
else if( Bytes >= mb && Bytes < gb )
sprintf(returnSize, "%.2f MB", (float)Bytes/mb);
else if( Bytes >= kb && Bytes < mb )
sprintf(returnSize, "%.2f KB", (float)Bytes/kb);
else if ( Bytes < kb)
sprintf(returnSize, "%.2f Bytes", Bytes);
else
sprintf(returnSize, "%.2f Bytes", Bytes);
return returnSize;
}

