Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Short Hand Decimal To Binary Vice Versa Good or Bad? (http://www.programmingforums.org/showthread.php?t=13991)

CIRCLE Sep 19th, 2007 6:46 PM

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


CIRCLE Sep 23rd, 2007 7:32 PM

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


DaWei Sep 23rd, 2007 8:57 PM

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



All times are GMT -5. The time now is 3:05 AM.

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