Solved

Conversion Binary <-> Decimal

Posted on 2002-04-12
7
83,252 Views
Last Modified: 2013-11-15
Hi
I need two simple functions:

1 - Convert Binary to Decimal and returns this value
2-  Convert Decimal to Binary and returns this value

Thanks
0
Comment
Question by:maomorta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:jonnin
ID: 6936650
dec to bin:
what I do is make a hex lookup table

0 = "0000"
1 = "0001"
...
f = "1111"

and do an sprintf into a hex string.
int i, char * str...
sprintf(str, "%x", i);

then each hex digit is replaced with the
lookup value. Kill leading zeros if you want too...

bin to dec is easier
place the bits into an unsigned int  using shift operator
<< ...


0
 
LVL 2

Accepted Solution

by:
jonnin earned 100 total points
ID: 6936656
come to think of it, you can shift the bits out instead of the sprintf thing...
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6936954
// Binary to Decimal
http://www.c-for-dummies.com/lessons/bonus/13/

// Decimal to Binary
http://mathforum.com/dr.math/problems/kaufman.10.26.99.html

I will try to make an example.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 6

Expert Comment

by:Triskelion
ID: 6937577
// Cheap example.  Microsoft compiler
#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));
}
0
 

Author Comment

by:maomorta
ID: 6938278
Hi jonnin,
As you sugested I wrote this program but I have a segmentation fault, and I can not understand where the problem is.

Can you give more details about
"bin to dec is easier
place the bits into an unsigned int  using shift operator
<< ..."

#include <stdio.h>
#include <string.h>

void int2bin(long num);

int main()
{    
     int2bin(12343);    
}


void int2bin(long num)
{
     char *hex;
     char bin[50]="";
     sprintf(hex, "%X", num);              
     while (*hex) {
          switch (*hex)
            {
                     case '0':
                             strcat(bin, "0000");                
                break;          
              case '1':
                   strcat(bin, "0001");                
                break;          
              case '2':
                   strcat(bin, "0010");
                break;
              case '3':
                   strcat(bin, "0011");
                break;
              case '4':
                   strcat(bin, "0100");
                break;
              case '5':
                   strcat(bin, "0101");
                break;
              case '6':
                   strcat(bin, "0110");
                break;
              case '7':
                   strcat(bin, "0111");
                break;
              case '8':
                   strcat(bin, "1000");
                break;
              case '9':
                   strcat(bin, "1001");
                break;
              case 'A':
                   strcat(bin, "1010");
                break;
              case 'B':
                   strcat(bin, "1011");
                break;
              case 'C':
                   strcat(bin, "1100");
                break;
              case 'D':
                   strcat(bin, "1101");
                break;
              case 'E':
                   strcat(bin, "1110");
                break;
              case 'F':
                   strcat(bin, "1111");
                break;                                
          }
                                                 
          *hex++;
     }
     
     printf("%s\n", bin);          
                         
}
0
 
LVL 1

Expert Comment

by:Shivshankar
ID: 6938532
Make these changes...

char * hex; ----> to  
    char * hex = (char *)(malloc(sizeof(long)*2*sizeof(char)));
Also, add this as a better programming practice.
    char *start_addr = hex;

*hex++;   -----> to
   hex++; //Just hex++

At the end of the code:
  free(start_addr);
0
 
LVL 18

Expert Comment

by:deighton
ID: 6942302
char *int2bin(long lno,char *buf)
{
     int c = 32;
     


     buf[32]=0;
     do
     {
         
          buf[--c] = '0' + (lno & 1);
          lno = lno >> 1;
     }
     while (c);
     

     return buf;

}    


long bin2int(char *buf)
{
     long val=0;
     int c=0;


     while (buf[c])
     {
          val = (val << 1) | (buf[c++]-'0');
     }
         
     return val;

}


'example

printf("\n%s",int2bin(1024,test));
printf("\n%li",bin2int(test));

0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is Ransomware? 16 105
Need software for photo capture from TV tuner card 20 108
Driver collector? 4 36
Weather Channel Nag Sreen Problem 6 38
If your app took Google’s lash recently, here are the 5 most likely reasons.
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
This video will demonstrate how to find the puppet warp tool from the edit menu and where to put the points to edit.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question