Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dec to bin

Dec to bin

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _AnsHUMAN_

    do you doubt it? why so Then check it for urself. I didn't try executing it and still it should work fine for all numbers that are within the int range whether it be 2 or 100.

    Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

    C Offline
    C Offline
    chandu004
    wrote on last edited by
    #5

    does it work for 1024?

    _ 2 Replies Last reply
    0
    • L Larsson

      How can I convert dec to bin? int i=123; printf("dec: %i bin: ???", i, i);

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #6

      check in This article[^] how i use a simple CRT function to print an integer in binary ;P here's an hint : ::itoa(the_int, the_dest_buffer, 2)[^];


      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      1 Reply Last reply
      0
      • C chandu004

        does it work for 1024?

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #7

        it won't work for that number because the value being returned is an int and range for int's is upto 32767 and while performing the calculations the number (value "binary" being returned from that function) exceeds the limits. ie why I said that this technique is quick and dirty :) . OPTIMIZE it

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        1 Reply Last reply
        0
        • C chandu004

          does it work for 1024?

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #8

          chandu004 wrote:

          does it work for 1024?

          void DecimalToBinary(int number) { int value; if(number <= 1) { cout << number; return; } value = number%2; DecimalToBinary(number >> 1); cout << value; } Now it would............ and now don't say that it won't work for a negative number :-> Just do some error handling. I hope that this does work for you :-O

          Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

          1 Reply Last reply
          0
          • _ _AnsHUMAN_

            Is it some kind of assignment to you? Anyways here is the code :

            int ConvertDecimalToBinary(int i)
            {
            	int temp = 0 ; 
            	int counter = 1; 
            	int binary = 0 ; 
            	while (0 != i)  
            	{
            		temp = i%2;
            		i = i/2;
            		binary += temp*counter;
            		counter*=10; 
            	}
            	return binary;
            }
            

            Larsson wrote:

            printf("dec: %i bin: ???", i, i);

            and yes, printf("dec: %d bin: %d", i, ConvertDecimalToBinary(i)); and this function does have its limitations. -- modified at 7:43 Wednesday 29th August, 2007

            Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #9

            did you know that itoa() was doing that kind of stuff by itself ? ;P


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            1 Reply Last reply
            0
            • _ _AnsHUMAN_

              Is it some kind of assignment to you? Anyways here is the code :

              int ConvertDecimalToBinary(int i)
              {
              	int temp = 0 ; 
              	int counter = 1; 
              	int binary = 0 ; 
              	while (0 != i)  
              	{
              		temp = i%2;
              		i = i/2;
              		binary += temp*counter;
              		counter*=10; 
              	}
              	return binary;
              }
              

              Larsson wrote:

              printf("dec: %i bin: ???", i, i);

              and yes, printf("dec: %d bin: %d", i, ConvertDecimalToBinary(i)); and this function does have its limitations. -- modified at 7:43 Wednesday 29th August, 2007

              Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #10

              _AnShUmAn_ wrote:

              and this function does have its limitations.

              It's a good thing, too. Otherwise you would have quickly realized your function does not "convert" its argument to base-2. The function will need to return some sort of char* or string in order to properly represent a base-2 number.


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              L 1 Reply Last reply
              0
              • _ _AnsHUMAN_

                Is it some kind of assignment to you? Anyways here is the code :

                int ConvertDecimalToBinary(int i)
                {
                	int temp = 0 ; 
                	int counter = 1; 
                	int binary = 0 ; 
                	while (0 != i)  
                	{
                		temp = i%2;
                		i = i/2;
                		binary += temp*counter;
                		counter*=10; 
                	}
                	return binary;
                }
                

                Larsson wrote:

                printf("dec: %i bin: ???", i, i);

                and yes, printf("dec: %d bin: %d", i, ConvertDecimalToBinary(i)); and this function does have its limitations. -- modified at 7:43 Wednesday 29th August, 2007

                Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                L Offline
                L Offline
                Larsson
                wrote on last edited by
                #11

                Thanks!

                1 Reply Last reply
                0
                • D David Crow

                  _AnShUmAn_ wrote:

                  and this function does have its limitations.

                  It's a good thing, too. Otherwise you would have quickly realized your function does not "convert" its argument to base-2. The function will need to return some sort of char* or string in order to properly represent a base-2 number.


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #12

                  This thread is like a fishing derby

                  M 2 Replies Last reply
                  0
                  • L led mike

                    This thread is like a fishing derby

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #13

                    :laugh:

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    • L led mike

                      This thread is like a fishing derby

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #14

                      I've noticed a pattern ... Given my timezone in relation to most posts, every morning I read through all last night's posts.  There's mostly posts with 10 to 100+ fishing-derby-style replies and then the one lonely reply by DavidCrow (or led mike) at the end with a one line simple solution :)  It's like a train wreck - I can't help but read them all LOL

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups