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
Prev Previous Post   Next Post Next

 
binary to decimal
Reply
Posted 2004-12-04, 01:43 PM
I need some help I need to write a program that a user inputs a string of 32 0's and 1's representing a binary number. then I need to convert the binary integer to its base 10 representation and then back.


Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


long     BinaryToDecimal(const char * pstrBinary)
{
     int     intLoop=0;
     long     lngReturn=0L;
     long     lngMultip=1L;
     for (intLoop=0; intLoop < (int)strlen(pstrBinary); intLoop++)
          {
          if ('1' == pstrBinary[intLoop])
            {
             lngReturn += lngMultip;
            }
          lngMultip *= 2;
          }
     return lngReturn;
}


char     *     DecimalToBinary(long lngDecimal)
{
     static     char strReturn[128]={0};
     _ltoa(lngDecimal, strReturn, 2);
     return strReturn;
}


int main(void)
{
     long          lngDecimal     =     7;
     char     *     pstrBinary     =     "111";
     printf("B2D=%ld\nD2B=%s\n", BinaryToDecimal(pstrBinary),DecimalToBinary(lngDecimal));
}

Last edited by deadlock75; 2004-12-04 at 02:09 PM.
Old
Profile PM WWW Search
deadlock75 is neither ape nor machine; has so far settled for the in-betweendeadlock75 is neither ape nor machine; has so far settled for the in-between
 
deadlock75
 



 

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 05:30 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.