Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Sep 19th, 2007, 5:46 PM   #1
CIRCLE
Newbie
 
Join Date: May 2007
Posts: 6
Rep Power: 0 CIRCLE is on a distinguished road
Short Hand Decimal To Binary Vice Versa Good or Bad?

Just wondering if there is an easier way like in C++.
This short bit of code opens a file and checks the binary number saved and closes the file. It then converts that number to decimal. Then it checks it against your current score from the end of the game and if it is higher reopens the file and converts that decimal number into binary and saves it to the file.

Is there an easier or even shorter way of doing this. Or is this about it.

void filewrite()
{
    highscoreint = 0;
    highscore = fopen("falling.sdr", "r");
    if (highscore == 0 )
    {
        highscore = fopen("falling.sdr", "w");
        fclose(highscore);
        return;
    }
    i = 0;
    while ((x = fgetc(highscore)) != EOF)
    {
        x -= 48;
        BinaryNum[i] = x;
        i++;
    } 
    for (i = 15, x = 0; i >= 0; i--, x++)
    {
        if (BinaryNum[i] == 1)
            highscoreint += pow(2, x);
        
    }

    textprintf_ex(activepage, font, 250, 240, makecol(0, 0, 0), -1, "High Score: %i", highscoreint);    
    if (highscoreint < score + totalscore)
        flag = 1;
    fclose(highscore);
    
    if (flag)
    {
        highscore = fopen("falling.sdr", "w+");
        if ((score + totalscore) > oldhighscoreint)
        {   
            highscoreint = oldhighscoreint;
            highscoreint = score + totalscore;
            for (i = 15; i >= 0; i--)
            {  
               PlaceHolder = pow(2, i);
               if ((highscoreint) >= PlaceHolder)
               {
                  highscoreint -= PlaceHolder;
                  BinaryNum[i] = 1;
               }
               else
                   BinaryNum[i] = 0;
            }
        }
        for (i = 15; i >= 0; i--)
            fprintf(highscore, "%i", BinaryNum[i]);
        fclose(highscore);
    }
}

Last edited by CIRCLE; Sep 19th, 2007 at 6:14 PM.
CIRCLE is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sample of my writing - Leave thoughts good or bad. bigguy Coder's Corner Lounge 3 Jun 9th, 2007 9:01 AM
C++ to C# vice versa jayme C++ 7 Nov 19th, 2005 8:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC