Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

what is the mistake in --->Bin2Dec code

Status
Not open for further replies.

tom_hanks

Full Member level 5
Joined
Aug 28, 2003
Messages
243
Helped
14
Reputation
28
Reaction score
1
Trophy points
1,298
Activity points
1,571
hi,
why is this code is not working?
Bin to Dec convertor....
------------------------------------------------------------------------------------------
int Bin2Dek(long * string)
{
unsigned long dioda[8];
unsigned int broj = 0;

dioda[8] = *string;

if(dioda[0] == 1) broj += 1;
if(dioda[1] == 1) broj += 2;
if(dioda[2] == 1) broj += 4;
if(dioda[3] == 1) broj += 8;
if(dioda[4] == 1) broj += 16;
if(dioda[5] == 1) broj += 32;
if(dioda[6] == 1) broj += 64;
if(dioda[7] == 1) broj += 128;

return broj;
}
 

You must compare strings like this

dioda[0] == '1'

try this if it gives again error report here.
 

hi there,
it is still not wrking..

i tried this way now..

int Bin2Dec(long * string,unsigned char digits)
{
unsigned long binar[15];
unsigned int broj = 0,i;
i= digits;
binar[15] = *string;
while(i> 0)
{
if(binar[0] == '1') broj += 1;
if(binar[1] == '1') broj += 2;
if(binar[2] == '1') broj += 4;
if(binar[3] == '1') broj += 8;
if(binar[4] == '1') broj += 16;
if(binar[5] == '1') broj += 32;
if(binar[6] == '1') broj += 64;
if(binar[7] == '1') broj += 128;
if(binar[8] == '1') broj += 256;
if(binar[9] == '1') broj += 512;
if(binar[10] == '1') broj += 1024;
if(binar[11] == '1') broj += 2048;
if(binar[12] == '1') broj += 4096;
if(binar[13] == '1') broj += 8192;
if(binar[14] == '1') broj += 16384;
if(binar[15] == '1') broj += 32768;
i--;
}
return (broj);


still not working...

Added after 1 hours 2 minutes:

can any one correct the code...

i will tick a help and donate 10 pnts..
 

Try this code

int Bin2Dec(long * string,unsigned int digits)
{
unsigned long binar[15];
unsigned int broj = 0,i;

binar[15] = *string;
for(i=0;i<digits+1;i++)
{
if(binar == '1') broj += pow(2,i);
}
return (broj);
}


PS: i think u'll need to add
#include<math.h>
 

my complier does not support any math command...

even after including the math file..

could u give some other example..
 

OK..

lets do a function for " x to the power y "

int Bin2Dec(long * string,unsigned int digits)
{
unsigned long binar[15];
unsigned int broj = 0,i;

binar[15] = *string;
for(i=0;i<digits+1;i++)
{
if(binar == '1') broj += power(2,i);
}
return (broj);
}

int power (int x, int y)
{
int k,z=1;
for (k=1;k<y+1;k++)
{
z=z*x;
}
return(z);
}

PS: dont forget to declare
int power (int , int );

Good Luck!
 

sir,
it is still not working...
 

tomhanks,

I did some modifications and here is the code WORKING


#include <stdio.h>


int Bin2Dec(int* ,int );
int power (int , int );
void main (void)
{
int x, dec;
int binary_num[100];
int digits;

printf("Please enter the number of digits of the binary number:");
scanf ("%d",&digits);

printf("Please enter the LSB of the binary number:");
for(x=0;x<digits;x++)
{
scanf ("%d",&binary_num[x]);
if ((binary_num[x]!=0) && (binary_num[x]!=1))
{
printf("ERROR..the binary number consists of 0's and 1's only");
return;
}
if (x<digits-1) printf("Please enter the next digit of the binary number:");
}


dec=Bin2Dec(binary_num,digits);
printf("The corresponding decimal number is %d \n", dec);
}


int Bin2Dec(int* binar,int digits)
{
int broj = 0,i;

for(i=0;i<digits+1;i++)
{
if(binar == 1) broj += power(2,i);
}
return (broj);
}

int power (int x, int y)
{
int k,z=1;
for (k=1;k<y+1;k++)
{
z=z*x;
}
return(z);
}


PS: enter the binary number digit by digit from right to left (from LSB to MSB)
 

I'm puzzled. Your original function inputs a pointer to a long integer. What exactly are you trying to calculate?

Try this - it converts an ASCII string of binary digits to an integer.

Code:
#include <stdio.h>

int binstring2int(char *s)
{
  int x = 0;

  while (*s)
    x = (x << 1) + (*s++ == '1');
  return x;
}

int main(void)
{
  char *s = "111010110111100110100010101";

  printf("binstring2int(\"%s\") = %d\n", s, binstring2int(s));
  return 0;
}
Output:
binstring2int("111010110111100110100010101") = 123456789
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top