Thread: conversion
View Single Post
  #3 (permalink)  
Old March 16th, 2004, 11:11 AM
gbilios gbilios is offline
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

I cant get the code to convert a negative binary number to its positive equivalent, which should add 1 and then convert to decimal. The main issue hgere is that I want the code to be able to turn the zeros into ones and the ones into zeros, egs, 10001100(negative) is converted to 01110011(positive)

#define SIZE 8

int toDecimal(int binary[])
{
int num[SIZE];
int decimal = 0;
int weight = 1;
int num;
int count;
for (count = SIZE -1;count >= 0; count--)
{
  if(binary[0] == '0') // is positive, convert to decimal
  {
         // num = binary % 10;
         num = binary[count];
         decimal = decimal + num * weight ;
         // binary = bianry / 10;
         weight = weight * 2;
   }
   else if (binary[0] == '1')
   {
     // is negative and must be converted to positive
     // equivalent, add 1, and then convert to decimal.
     //....
} // end for
return decimal;
} // end function



George N. Bilios
Reply With Quote