Convert decimal to binary

want code for decimal to binary

Page 1 of 1

7 Replies - 16436 Views - Last Post: 06 January 2009 - 12:09 AM Rate Topic: ****- 1 Votes

#1 learningC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-January 09

Convert decimal to binary

Post icon  Posted 05 January 2009 - 05:33 AM

hey guys i had to convert an array of decimal integers to binary integers. please post the code for code only converting the a binary no. to integer. i have read earlier posts but dont make use of other than userdefined functions and. just give the code using loops and mod instead of using already given functions and please give the inverse of it and also hexa and octa
Is This A Good Question/Topic? 0
  • +

Replies To: Convert decimal to binary

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Convert decimal to binary

Posted 05 January 2009 - 05:43 AM

Click here for more information.
And search here for snippets available.

That's all I can say after reading your brief post [You could have at least mentioned which language you are working on! C or C++?].
Was This Post Helpful? 0
  • +
  • -

#3 learningC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-January 09

Re: Convert decimal to binary

Posted 05 January 2009 - 05:47 AM

View PostAmitTheInfinity, on 5 Jan, 2009 - 04:43 AM, said:

Click here for more information.
And search here for snippets available.

That's all I can say after reading your brief post [You could have at least mentioned which language you are working on! C or C++?].



Sorry i forgot to mention i am working on c. but the abt pasting the code i am simply confused i am completly blank about it. i hope you understand. i am not tht good in programming
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Convert decimal to binary

Posted 05 January 2009 - 06:07 AM

Well, school assignments are not just to be handed in, the purpose is to learn something from them.
We're not just handing out code, people expect to be paid for that kind of service. We do however help out if you get stuck but only after you've showed some serious effort first and we do that FOR FREE.
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Convert decimal to binary

Posted 05 January 2009 - 09:26 AM

View PostGloin, on 5 Jan, 2009 - 05:07 AM, said:

Well, school assignments are not just to be handed in, the purpose is to learn something from them.
We're not just handing out code, people expect to be paid for that kind of service. We do however help out if you get stuck but only after you've showed some serious effort first and we do that FOR FREE.


Correction: "We" = Goblin

Here's your answer:
/********************************************************/
/*                    Binary converter                  */
/*                     By Matt Fowler                   */
/*                philosopher150@yahoo.com              */
/*  converts text into binary using the division method */
/*                   through ASCII code                 */
/*compiled with the Dev-C++ compiler (www.bloodshed.net)*/
/********************************************************/

#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>

char *entry, letter, choice[2];
int ascii, len, binary[8], total;
void prog();

int main()
{
      prog();     
      return 0;
}        
        
void prog()
{
   entry = new char[501]; 
   /* entry should be dynamic, otherwise a new 
      string entry of 501 chars would be created
      each time function is called!  
      Talk about memory hog! */
   cout<<"Enter string to convert (up to 500 chars): ";
   cin.getline(entry, 500);
   len = strlen(entry);  /* get the number of characters in entry. */
   /* this loop is executed for each letter in the string. */
   for(int i = 0; i<len; i++)
   {
      total = 0;
      letter = entry[i]; /* store the first letter */
      ascii = letter;    /* put that letter into an int, so we can 
                            see its ASCII number */ 
      while(ascii>0) /* This while loop converts the ASCII # into binary,
                        stores it backwards into the binary array. */
      {
         /* To get the binary code one must take the decimal number in
            question, take it and divide it by two repeatedly, save
            the remainder (which will become the binary number), save
            the whole number, divide by two, and repeat the whole
            process until 0 is reached.  This if-else statement serves
            this functionality, by getting the remainder of the ascii
            code, storing it in the array and then dividing the int
            ascii by two */
         if((ascii%2)==0)
         {
            binary[total] = 0;
            ascii = ascii/2;
            total++; /* increasing by one each time will yeild the
                        number of numbers in the array. */
         }
         else
         {
            binary[total] = 1;
            ascii = ascii/2;
            total++;
         }
      }
      total--; /* due to data type factors, the program will actually
                  add a 0 at the end of the array that is not supposed
                  to be there, decrementing total will solve this
                  problem, as that 0 will not be displayed. */
      /* this while loop displays the binary code for that letter. */
      while(total>=0)
      {
         cout<<binary[total];
         total--;
      }
   }
   delete[] entry; /* free up the memory used by entry */
   cout<<endl<<"Do again(1 = yes, 2= no)?: ";
   cin.getline(choice,3);
   if(choice[0] == '1')
      prog(); /* program is recursive, it calls itself.  It's kinda
                 like a function loop of sorts. */
   else
      exit(0); /* quits the program */  
}


Welcome, hope it helps (more or less, hope you see how it works instead of just copying & pasting without ever reading/modifying/trying to duplicate from scratch using this as aid when you're 100% STUMPED on duplicating it/creating your own method(s) of it)!
Was This Post Helpful? 0
  • +
  • -

#6 learningC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-January 09

Re: Convert decimal to binary

Posted 05 January 2009 - 11:20 AM

View PostHyper, on 5 Jan, 2009 - 08:26 AM, said:

View PostGloin, on 5 Jan, 2009 - 05:07 AM, said:

Well, school assignments are not just to be handed in, the purpose is to learn something from them.
We're not just handing out code, people expect to be paid for that kind of service. We do however help out if you get stuck but only after you've showed some serious effort first and we do that FOR FREE.


Correction: "We" = Goblin

Here's your answer:
/********************************************************/
/*                    Binary converter                  */
/*                     By Matt Fowler                   */
/*                philosopher150@yahoo.com              */
/*  converts text into binary using the division method */
/*                   through ASCII code                 */
/*compiled with the Dev-C++ compiler (www.bloodshed.net)*/
/********************************************************/

#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>

char *entry, letter, choice[2];
int ascii, len, binary[8], total;
void prog();

int main()
{
      prog();     
      return 0;
}        
        
void prog()
{
   entry = new char[501]; 
   /* entry should be dynamic, otherwise a new 
      string entry of 501 chars would be created
      each time function is called!  
      Talk about memory hog! */
   cout<<"Enter string to convert (up to 500 chars): ";
   cin.getline(entry, 500);
   len = strlen(entry);  /* get the number of characters in entry. */
   /* this loop is executed for each letter in the string. */
   for(int i = 0; i<len; i++)
   {
      total = 0;
      letter = entry[i]; /* store the first letter */
      ascii = letter;    /* put that letter into an int, so we can 
                            see its ASCII number */ 
      while(ascii>0) /* This while loop converts the ASCII # into binary,
                        stores it backwards into the binary array. */
      {
         /* To get the binary code one must take the decimal number in
            question, take it and divide it by two repeatedly, save
            the remainder (which will become the binary number), save
            the whole number, divide by two, and repeat the whole
            process until 0 is reached.  This if-else statement serves
            this functionality, by getting the remainder of the ascii
            code, storing it in the array and then dividing the int
            ascii by two */
         if((ascii%2)==0)
         {
            binary[total] = 0;
            ascii = ascii/2;
            total++; /* increasing by one each time will yeild the
                        number of numbers in the array. */
         }
         else
         {
            binary[total] = 1;
            ascii = ascii/2;
            total++;
         }
      }
      total--; /* due to data type factors, the program will actually
                  add a 0 at the end of the array that is not supposed
                  to be there, decrementing total will solve this
                  problem, as that 0 will not be displayed. */
      /* this while loop displays the binary code for that letter. */
      while(total>=0)
      {
         cout<<binary[total];
         total--;
      }
   }
   delete[] entry; /* free up the memory used by entry */
   cout<<endl<<"Do again(1 = yes, 2= no)?: ";
   cin.getline(choice,3);
   if(choice[0] == '1')
      prog(); /* program is recursive, it calls itself.  It's kinda
                 like a function loop of sorts. */
   else
      exit(0); /* quits the program */  
}


Welcome, hope it helps (more or less, hope you see how it works instead of just copying & pasting without ever reading/modifying/trying to duplicate from scratch using this as aid when you're 100% STUMPED on duplicating it/creating your own method(s) of it)!



hey thanks for your help but i appreciate it. its not my school assingment . i am an indian and indian education system sucks. its jus for my interest i am learing programming. i really apprecite your help but just want to tell you can you give it using just iostream.h and no other header file and stuff. IN SIMPLE WORDS JUST MAKE IT USING USER DFINED FUCNTIONS AND IOSTREAM.H. COZ I HAVENT REACHED THERE.
Was This Post Helpful? 0
  • +
  • -

#7 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Convert decimal to binary

Posted 05 January 2009 - 11:58 AM

You're very welcome for the help.

If my reply was helpful, please click on the: This Was A Helpful Post! button.
Was This Post Helpful? 1
  • +
  • -

#8 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Convert decimal to binary

Posted 06 January 2009 - 12:09 AM

I would like to make one more correction here...

WE = DIC. [Rule No. 1]

DIC has a policy about not handing over code to user until user shows some sort of effort first. I hope Hyper knows it. And yes, as I said earlier, there are more than one

http://www.dreaminco.../snippet903.htm
http://www.dreaminco.../snippet689.htm
http://www.dreaminco.../snippet942.htm
http://www.dreaminco.../snippet585.htm
http://www.dreaminco.../snippet771.htm
http://www.dreaminco.../snippet888.htm
http://www.dreaminco.../snippet882.htm
http://www.dreaminco...snippet2513.htm
etc...

snippets available on this problem. So if user had few efforts on search, I don't think there was need of this post.

@learningC
If your problem has been resolved, please post what was the final solution, it might help other people when they will be searching for similar answers.

This post has been edited by AmitTheInfinity: 06 January 2009 - 12:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1