Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   [C++]Binary -> Decimal (http://zelaron.com/forum/showthread.php?t=27100)

Acer 2004-01-30 07:46 PM

[C++]Binary -> Decimal
 
Another program I wrote to help someone new...
It was quickly written

Code:

#include "stdafx.h"
#include <windows.h>
#include <iostream.h>

int toPower2(int value){
        int rtnvle = 1;
        if(value > 1)
    for(int i=2;i<=value;i++){
        rtnvle *= 2;
        }
        return rtnvle;
}

int main(int argc, char* argv[])
{
        char binary_value[16];
        int  decimal = 0;

    cout<<"Type in a binary value 16 max(remember binary is right to left): ";
        cin>>binary_value;
       
    for(int i=strlen(binary_value) -1;i >= 0;i--){
                if(binary_value[i] == '1'){
                        decimal += toPower2(strlen(binary_value) - i);
                }
            else if(binary_value[i] != '0')
                        return 0;
        }

    cout<<decimal<<endl;
        return 0;
}

PS: I wanted to use pointers but that would of been to complex for the person I was teaching

pr0xy 2004-02-01 12:41 AM

Ok if you can't convert binary to decimal and binary to hex and hex to decimal you should not be permited to live =[

Acer 2004-02-01 06:19 AM

Quote:

Originally Posted by pr0xy
Ok if you can't convert binary to decimal and binary to hex and hex to decimal you should not be permited to live =[

it isnt really for converting... its just source so people can learn some C++


All times are GMT -6. The time now is 06:39 PM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.