Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created April 24, 2014 15:21
Show Gist options
  • Save mortymacs/11258602 to your computer and use it in GitHub Desktop.
Save mortymacs/11258602 to your computer and use it in GitHub Desktop.
convert number to human-readable size in C
#include <stdio.h>
char *types[] = {"KB","MB","GB","TB","PB","EB","ZB","YB"};
void change(int *size)
{
int div = 1024;
int i = 0;
for(;i<8;i++)
{
*size /= div;
if(*size < div)
{
printf("%d %s\n",*size,*(types+i));
break;
}
}
}
void main()
{
int size = 2048;
change(&size);
size = 225614;
change(&size);
size = 225614000;
change(&size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment