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

  • Home
  • SEARCH
  • 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 842569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:59:05+00:00 2026-05-15T05:59:05+00:00

Is there a simple way to do something like.. [NSMagicDataConverter humanStringWithBytes:20000000] ..which would return

  • 0

Is there a simple way to do something like..

[NSMagicDataConverter humanStringWithBytes:20000000]

..which would return “19.1MB”?

  • 1 1 Answer
  • 0 Views
  • 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-15T05:59:06+00:00Added an answer on May 15, 2026 at 5:59 am

    Here’s my own take on the problem:

    enum {
        kUnitStringBinaryUnits     = 1 << 0,
        kUnitStringOSNativeUnits   = 1 << 1,
        kUnitStringLocalizedFormat = 1 << 2
    };
    
    NSString* unitStringFromBytes(double bytes, uint8_t flags){
    
        static const char units[] = { '\0', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
        static int maxUnits = sizeof units - 1;
    
        int multiplier = (flags & kUnitStringOSNativeUnits && !leopardOrGreater() || flags & kUnitStringBinaryUnits) ? 1024 : 1000;
        int exponent = 0;
    
        while (bytes >= multiplier && exponent < maxUnits) {
            bytes /= multiplier;
            exponent++;
        }
        NSNumberFormatter* formatter = [[[NSNumberFormatter alloc] init] autorelease];
        [formatter setMaximumFractionDigits:2];
        if (flags & kUnitStringLocalizedFormat) {
            [formatter setNumberStyle: NSNumberFormatterDecimalStyle];
        }
        // Beware of reusing this format string. -[NSString stringWithFormat] ignores \0, *printf does not.
        return [NSString stringWithFormat:@"%@ %cB", [formatter stringFromNumber: [NSNumber numberWithDouble: bytes]], units[exponent]];
    }
    

    By default (if 0 is passed for flags), it will output SI units (base ten). You can set kUnitStringBinaryUnits to select binary (base two) units suitable for memory, or kUnitStringOSNativeUnits to have the unit type selected automatically based on OS version (pre-Leopard gets base two, post-Leopard gets base ten). Setting kUnitStringLocalizedFormat formats the string based on the user’s current locale. For example:

    unitStringFromBytes(1073741824, 0); // → "1.07 GB"
    unitStringFromBytes(1073741824, kUnitStringBinaryUnits); // → "1 GB"
    unitStringFromBytes(1073741824, kUnitStringOSNativeUnits | kUnitStringLocalizedFormat); // → "1.07 GB" (In Mac OS 10.6)
    unitStringFromBytes(12345678901234567890123456789, kUnitStringOSNativeUnits | kUnitStringLocalizedFormat); // → "12,345.68 YB" (In Mac OS 10.6, in the US)
    unitStringFromBytes(12345678901234567890123456789, kUnitStringOSNativeUnits | kUnitStringLocalizedFormat); // → "12.345,68 YB" (In Mac OS 10.6, in Spain)
    

    Here’s the helper function required for OS-native units:

    BOOL leopardOrGreater(){
        static BOOL alreadyComputedOS = NO;
        static BOOL leopardOrGreater = NO;
        if (!alreadyComputedOS) {
            SInt32 majorVersion, minorVersion;
            Gestalt(gestaltSystemVersionMajor, &majorVersion);
            Gestalt(gestaltSystemVersionMinor, &minorVersion);
            leopardOrGreater = ((majorVersion == 10 && minorVersion >= 5) || majorVersion > 10);
            alreadyComputedOS = YES;
        }
        return leopardOrGreater;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 424k
  • Answers 424k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use a couple static methods instead: public class Parser {… May 15, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer SELECT count(*) FROM active WHERE timestamp > unix_timestamp() - 1800… May 15, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer try the give query. it may help. Top 20 give… May 15, 2026 at 11:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.