Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
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
Old Sep 23rd, 2007, 6:32 PM   #2
CIRCLE
Newbie
 
Join Date: May 2007
Posts: 6
Rep Power: 0 CIRCLE is on a distinguished road
With out all the game code this is what it looks like.

#include <stdio.h>
#include <math.h>

int BinaryNum[50];
long ExpLoop;
long PlaceHolder;
long UserNumber;

int main()
{
    printf("Give me a Number to Convert to Binary.  Up to 65536_\n");
    scanf("%i", &UserNumber);
    for (ExpLoop = 15; ExpLoop > 0; ExpLoop--)
        BinaryNum[ExpLoop] = 0;
    for (ExpLoop = 15; ExpLoop >= 0; ExpLoop--)
    {  
        PlaceHolder = pow(2, ExpLoop);
        if (UserNumber >= PlaceHolder)
        {
            UserNumber -= PlaceHolder;
            BinaryNum[ExpLoop] = 1;
        }
    }
    for (ExpLoop = 15; ExpLoop >= 0; ExpLoop--)
        printf("%i", BinaryNum[ExpLoop]);
        
    for (ExpLoop = 0; ExpLoop <= 15; ExpLoop++)
    {
        if (BinaryNum[ExpLoop] == 1)
            UserNumber += pow(2, ExpLoop);
    }   
    printf("\n The Number You used was %i", UserNumber); 
    getch();
    return 0;
}
CIRCLE is offline   Reply With Quote
Old Sep 23rd, 2007, 7:57 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
This seems simpler, plus it checks the input for success, which one should always do:
#include <iostream>

int main (int argc, char* argv[])
{
    unsigned theNumber;
    unsigned theLength = sizeof (unsigned) * 8;
    unsigned mask = 1 << theLength - 1;

    std::cout << "Enter a number: ";
    if (scanf ("%d", &theNumber) != 1)
    {
        std::cout << "Bad input, dink" << std::endl;
        return 1;
    }
    for (int i = 0; i < theLength; i++)
    {
        if (theNumber & mask) std::cout << '1'; else std::cout << '0';
        mask >>= 1;
    }
    std::cout << endl;
    return 0;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

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 2:32 AM.

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