![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: May 2007
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: May 2007
Posts: 6
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |