Results 1 to 6 of 6
  1. #1
    pDevice's Avatar
    Join Date
    Feb 2012
    Gender
    male
    Location
    d3d9.h
    Posts
    1,306
    Reputation
    15
    Thanks
    420
    My Mood
    Stressed

    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;
             }



  2. The Following User Says Thank You to pDevice For This Useful Post:

    abuckau907 (04-05-2014)

  3. #2
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    You're returning the address of a local variable of the function.

    Char* x = function(4000);
    Char* y = function(8000);

    Both point to the same char array. Not what you'd expect.

  4. #3
    pDevice's Avatar
    Join Date
    Feb 2012
    Gender
    male
    Location
    d3d9.h
    Posts
    1,306
    Reputation
    15
    Thanks
    420
    My Mood
    Stressed
    Quote Originally Posted by abuckau907 View Post
    You're returning the address of a local variable of the function.

    Char* x = function(4000);
    Char* y = function(8000);

    Both point to the same char array. Not what you'd expect.
    Works fine for me



  5. #4
    Fovea's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    325
    Reputation
    101
    Thanks
    411
    My Mood
    Amused
    It works fine because the program you have written for verification is simple. In the real world, your function will break because what is returned exists on the stack and it will be overwritten eventually.

  6. #5
    Harava's Avatar
    Join Date
    Sep 2013
    Gender
    male
    Posts
    114
    Reputation
    10
    Thanks
    2,989
    Fix'd.

    Code:
    #include <cstdio>
    #include <windows.h>
    
    bool BytesToSize( double Bytes , char * ByteString , int BufSize )
    {
        float tb = 1099511627776.0f;
        float gb = 1073741824.0f;
        float mb = 1048576.0f;
        float kb = 1024.0f;
    
        char * returnSize = (char *)malloc(BufSize);
        if(returnSize == NULL)
            return false;
    
        if( Bytes >= tb )
            sprintf(returnSize, "%.2lf TB", (double)Bytes/tb);
        else if( Bytes >= gb && Bytes < tb )
            sprintf(returnSize, "%.2lf GB", (double)Bytes/gb);
        else if( Bytes >= mb && Bytes < gb )
            sprintf(returnSize, "%.2lf MB", (double)Bytes/mb);
        else if( Bytes >= kb && Bytes < mb )
            sprintf(returnSize, "%.2lf KB", (double)Bytes/kb);
        else if ( Bytes < kb)
            sprintf(returnSize, "%.2lf Bytes", Bytes);
        else
            sprintf(returnSize, "%.2lf Bytes", Bytes);
    
        memcpy((void *)ByteString, (void *)returnSize, BufSize);
        free(returnSize);
        return true;
    }
    
    int main()
    {
        double baits = 9999999999999999999999.0f;
        char ByteString[512];
        if(BytesToSize(baits, ByteString, sizeof(ByteString)))
            printf("%lf Bytes are %s \n", baits, ByteString);
        return 0;
    }

  7. The Following User Says Thank You to Harava For This Useful Post:

    abuckau907 (04-05-2014)

  8. #6
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    Thanks @Harava


    @OP
    One more thing ..

    if( Bytes >= tb )
    ...
    else if( Bytes >= gb && Bytes < tb )
    ...
    else if( Bytes >= mb && Bytes < gb )
    ...
    else if( Bytes >= kb && Bytes < mb )
    ...
    else if ( Bytes < kb)
    ...
    else
    If the first IF branch is not taken, that means bytes < tb --> No need to test for bytes < tb in the 2nd if clause.
    Same thing continues down -- all the conditions to the right of && were proven true in the previous if statement : P

    if( Bytes >= tb )
    ...
    else if( Bytes >= gb)
    ...
    else if( Bytes >= mb)
    ...
    else if( Bytes >= kb )
    ...
    else
    Last edited by abuckau907; 04-05-2014 at 12:14 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

Similar Threads

  1. [Help Request] Converting a String( "10") into a Byte ("10")
    By Nordiii in forum Call of Duty Modern Warfare 3 Coding, Programming & Source Code
    Replies: 8
    Last Post: 01-09-2013, 11:15 AM
  2. [Help Request] Show Players Name/Byte to String
    By true1495 in forum Call of Duty Modern Warfare 3 Coding, Programming & Source Code
    Replies: 16
    Last Post: 10-10-2012, 05:53 AM
  3. [Tutorial]Convert Bits 2 Bytes
    By NextGen1 in forum Visual Basic Programming
    Replies: 7
    Last Post: 01-19-2010, 08:53 PM
  4. Convert Byte to Mb
    By Zoom in forum Visual Basic Programming
    Replies: 5
    Last Post: 01-11-2010, 09:08 AM
  5. Converting Assembly Into Bytes
    By radnomguywfq3 in forum Visual Basic Programming
    Replies: 0
    Last Post: 09-24-2007, 04:42 PM