Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   binary to decimal (http://zelaron.com/forum/showthread.php?t=34836)

deadlock75 2004-12-04 12:43 PM

binary to decimal
 
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));
}


WetWired 2004-12-05 11:14 AM

What exactly is the problem?


All times are GMT -6. The time now is 04:22 PM.

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