pastebin - collaborative debugging tool
kpaste.net RSS


C friendly filesizes
Posted by Admin on Sat 4th Aug 2012 01:41
raw | new post

  1. #include <stdio.h>
  2.  
  3. void mksize (float bytes, char *ret) {
  4.         if (bytes < 1024)
  5.                 sprintf(ret, "%.f B", bytes);
  6.         else if (bytes < 1048576)
  7.                 sprintf(ret, "%.2f kB", bytes / 1024);
  8.         else if (bytes < 1073741824)
  9.                 sprintf(ret, "%.2f MB", bytes / 1048576);
  10.         else if (bytes < 1099511627776)
  11.                 sprintf(ret, "%.2f GB", bytes / 1073741824);
  12.         else
  13.                 sprintf(ret, "%.2f TB", bytes / 1099511627776);
  14. }
  15.  
  16.  
  17. void main() {
  18.         char out[255];
  19.         mksize(1572864, out);
  20.         printf("%s\n", out);
  21. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at