c program

program to convert binary to decimal

Page 1 of 1

4 Replies - 1800 Views - Last Post: 30 January 2009 - 07:46 AM Rate Topic: -----

#1 sathya_c_program   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-January 09

c program

Post icon  Posted 30 January 2009 - 05:39 AM

program to convert binary to decimal


Is This A Good Question/Topic? 0
  • +

Replies To: c program

#2 Lillefix   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: c program

Posted 30 January 2009 - 05:50 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 Plus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: c program

Posted 30 January 2009 - 06:33 AM

best case is that you bring the binary number (8 bits) in boolean [8]

 bool Byte[8] = 0; 


say hello to boolean world .. and Logic Gates ...

now you can use this array to read the binary
and convert it into decimal using a function ...
Was This Post Helpful? 0
  • +
  • -

#4 hamidkhl   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: c program

Posted 30 January 2009 - 06:53 AM

View Postsathya_c_program, on 30 Jan, 2009 - 04:39 AM, said:

program to convert binary to decimal



Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.



Thank you for helping us helping you.
Was This Post Helpful? 0
  • +
  • -

#5 Linkowiezi   User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: c program

Posted 30 January 2009 - 07:46 AM

Well, this is one way of doing it in C++. So if you really want it it C you have to change a few things for it to work. Not much, but a little bit...

It also has LONG variable names :P
But they might be descriptive anyway. Didn't wanna mess with yer head and obfuscate it to much.

But you should get a hang of how to make it in C yourself if you just read what everything does.
Here goes:
#include <iostream>
#define printToConsoleScreen cout
#define readFromConsoleScreen cin
int userInput(){
  int userInputInteger;
  std::printToConsoleScreen << "Enter number to turn into binary (negative number to quit): ";
  std::readFromConsoleScreen >> userInputInteger;
  return userInputInteger;
}
int main(int argc, char *argv[]){
  const int THISISAMAXIMUMVALUECONSTANT = 16;
  int userInputInteger;
  char cString[THISISAMAXIMUMVALUECONSTANT+1];
  char temporaryCharacter;
  userInputInteger = userInput();
  while( userInputInteger >= 0 ){
    int temporaryInteger=userInputInteger, valueOfSomeIntegerUsedInSomeWay=-1;
    for( int integerUsedInForLoop = THISISAMAXIMUMVALUECONSTANT-1; integerUsedInForLoop >= 0; integerUsedInForLoop--, temporaryInteger=temporaryInteger/2){
      valueOfSomeIntegerUsedInSomeWay=temporaryInteger%2;
      itoa(valueOfSomeIntegerUsedInSomeWay, &temporaryCharacter, 2);
      cString[integerUsedInForLoop] = temporaryCharacter;
    }
    cString[THISISAMAXIMUMVALUECONSTANT] = '\0';
    std::printToConsoleScreen << userInputInteger << " = " << cString << std::endl;
    userInputInteger = userInput();
  }
  std::printToConsoleScreen << "Have a happy life!" << std::endl;
  std::readFromConsoleScreen.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
  std::readFromConsoleScreen.get();
  return 0;
}


EDIT: It doesn't eat away the preceding zeroes tough, so you might want to try to fix that yourself to ;)

Hope this was of any help to you :D

This post has been edited by Linkowiezi: 30 January 2009 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1