PDA

View Full Version : [C++]Binary -> Decimal


Acer
2004-01-30, 07:46 PM
Another program I wrote to help someone new...
It was quickly written


#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
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++