Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Community Calendar Today's Posts Search
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art > Tech Help

 
 
Thread Tools Display Modes

 
[C++]Binary -> Decimal
Reply
Posted 2004-01-30, 07:46 PM
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
Old
Profile PM WWW Search
Acer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzAcer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Acer
 



 
Reply
Posted 2004-02-01, 12:41 AM in reply to Acer's post "[C++]Binary -> Decimal"
Ok if you can't convert binary to decimal and binary to hex and hex to decimal you should not be permited to live =[
Old
Profile PM WWW Search
pr0xy is neither ape nor machine; has so far settled for the in-betweenpr0xy is neither ape nor machine; has so far settled for the in-between
 
 
pr0xy
 



 
Reply
Posted 2004-02-01, 06:19 AM in reply to pr0xy's post starting "Ok if you can't convert binary to..."
pr0xy said:
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++
Old
Profile PM WWW Search
Acer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzAcer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Acer
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules [Forum Rules]
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -6. The time now is 03:31 AM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.