|
||
|
GP Mailing List
ATXGPSIG List
|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: My brain's getting rusty - binary conversion
On Tue, 1 Jul 1980, Lionel Pinkhard wrote:
> Hi,
>
> I'm sorry, my brain seems to be getting a bit rusty. I've been working
all day trying to write a simple program to convert a binary number into
decimal. I tried inputting the number with 'cin', multiplying each
digit by 128, 64, 32, 16, 8, 4, 2, 1, respectively, and then
outputting it with 'cout', but every time I get another number than if
I convert it by hand. Now I know my conversion (by hand) may be incorrect,
but I don't think that 00000001 should be equal to 0. Another problem is
that my code is pretty long. Can somebody please give me a short
formula for doing this translation, in either C, C++, Fortran 77
or Linux Assembler? Another language would also do fine if you tell me where
to get a compiler and some tutorials on how to use it. :-) I also have
Chipmunk Basic, but I know how limited Basic is.
>
> Also, if somebody could give me a formula for translating from decimal to
binary, I would be grateful. I know this is much more trickier, and I have no
idea how to use either powers or square roots in C++. If somebody would tell
me this, it would also help.
>
> Kind regards,
>
> Lionel Pinkhard
> =================================================================
How about (I think they work) :
//bin2dec
char c[sizeof(unsingned long)*8+1],i;//number to convert
unsigned long result=0;
for(i=0;i<sizeof(unsigned long)*8;i++){
result=result<<1;
if(c[i]!='0')result+=1;
}
//dec2bin
unsigned long number;//number to convert
char result[sizeof(unsinged long)*8+1],i;
for(i=sizeof(unsigned long)*8-1;i>=0;i--){
{
if(number&1==1)result[i]='1';else
result[i]='0';
number=number>>1;
}
result[sizeof(unsigned long)*8]=0;
Hope this helps,
Cosmin.
=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once
To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html
|
|