Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3222114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:57:14+00:00 2026-05-17T15:57:14+00:00

I want to convert a single number of bytes, into a file size (that

  • 0

I want to convert a single number of bytes, into a file size (that has .KB, .MB and .GB).

If the number is 0, I don’t want to have any unit.
If the number is exactly divisible by a multiple of 1024 (not a floating point), then I will print: x . Otherwise, I want to print a floating point with one degree precision.

I made some code that seems to work well, but it’s very cumbersome. I’m looking into ways I could make my function cleaner/more efficient please, it’s honestly VERY ugly:

char *
calculateSize( off_t size )
{
  char *result = (char *) malloc(sizeof(char) * 20);
  static int GB = 1024 * 1024 * 1024;
  static int MB = 1024 * 1024;
  static int KB = 1024;
  if (size >= GB) {
    if (size % GB == 0)
      sprintf(result, "%d GB", size / GB);
    else
      sprintf(result, "%.1f GB", (float) size / GB);
  }
  else if (size >= MB) {
    if (size % MB == 0)
      sprintf(result, "%d MB", size / MB);
    else
      sprintf(result, "%.1f MB", (float) size / MB);
  }
  else {
    if (size == 0) {
      result[0] = '0';
      result[1] = '\0';
    }
    else {
      if (size % KB == 0)
        sprintf(result, "%d KB", size / KB);
      else
        sprintf(result, "%.1f KB", (float) size / KB);
    }
  }
  return result;
}

I would really appreciate if someone has a better way to achieve the same result please.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:57:15+00:00Added an answer on May 17, 2026 at 3:57 pm

    Using a table-driven representation extended up to EiB.

    #include <inttypes.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define DIM(x) (sizeof(x)/sizeof(*(x)))
    
    static const char     *sizes[]   = { "EiB", "PiB", "TiB", "GiB", "MiB", "KiB", "B" };
    static const uint64_t  exbibytes = 1024ULL * 1024ULL * 1024ULL *
                                       1024ULL * 1024ULL * 1024ULL;
    
    char *
    calculateSize(uint64_t size)
    {   
        char     *result = (char *) malloc(sizeof(char) * 20);
        uint64_t  multiplier = exbibytes;
        int i;
    
        for (i = 0; i < DIM(sizes); i++, multiplier /= 1024)
        {   
            if (size < multiplier)
                continue;
            if (size % multiplier == 0)
                sprintf(result, "%" PRIu64 " %s", size / multiplier, sizes[i]);
            else
                sprintf(result, "%.1f %s", (float) size / multiplier, sizes[i]);
            return result;
        }
        strcpy(result, "0");
        return result;
    }
    

    Test code

    int main(void)
    {   
        uint64_t list[] =
        {   
            0, 1, 2, 34, 900, 1023, 1024, 1025, 2048, 1024 * 1024, 
            1024 * 1024 * 1024 + 1024 * 1024 * 400
        };
        int i; 
        for (i = 0; i < DIM(list); i++)
        {   
            char *str = calculateSize(list[i]);
            printf("%18" PRIu64 " = %s\n", list[i], str);
            free(str);
        }
        return 0;
    }
    

    Test output

                     0 = 0
                     1 = 1 B
                     2 = 2 B
                    34 = 34 B
                   900 = 900 B
                  1023 = 1023 B
                  1024 = 1 KiB
                  1025 = 1.0 KiB
                  2048 = 2 KiB
               1048576 = 1 MiB
            1493172224 = 1.4 GiB
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an IEnumerable<IEnumerable<T>> collection that I want to convert to a single dimension
I have a small UIImage (jpg) with a single typed number. I want to
I want to convert a sequence of numbers to a single number which will
I have a lot of PDF documents that I want to convert to PNG,
I have a single column in length of R*N, I want to convert it
I want to convert a column of my dataframe into another one. I have
I want to convert a number to hex and store the result in a
I want to convert plaintext to link for an input I have. The HTML
I want to convert XML into binary data in Java? What is the fastest
I want to convert a 44,100 Hz, 24 Bit Mono wav file to aformat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.