how to convert decimal number to binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #16
    hehe actually if you look (and if I had added in a section for every bit) they would print out in the correct order.

    And I never claimed to be good at c/c++, just saying it didn't look like anyone had given a solid answer to the post

    Comment

    • fantasticamir
      New Member
      • Apr 2007
      • 42

      #17
      bitset<length> myBinary ((long) <your number>) ;

      cout<<myBinary;

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #18
        Originally posted by fantasticamir
        bitset<length> myBinary ((long) <your number>) ;

        cout<<myBinary;
        Hey, didn't know that one. Learn something every day. ;)


        Adrian

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #19
          Originally posted by AdrianH
          Hey, didn't know that one. Learn something every day. ;)


          Adrian
          I suppose that we exist to learn.

          ;)

          Savage

          Comment

          • WestCoast101
            New Member
            • Jul 2007
            • 2

            #20
            Originally posted by fantasticamir
            bitset<length> myBinary ((long) <your number>) ;

            cout<<myBinary;
            GREAT: Two questions.
            code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.

            Comment

            • WestCoast101
              New Member
              • Jul 2007
              • 2

              #21
              Originally posted by fantasticamir
              bitset<length> myBinary ((long) <your number>) ;

              cout<<myBinary;
              GREAT: Two questions.
              First, the code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.

              Second, is there a reference text/site which has indexed code snippets for math/engineering C++.

              TIA

              WestCoast101

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #22
                Originally posted by WestCoast101
                GREAT: Two questions.
                First, the code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.

                Second, is there a reference text/site which has indexed code snippets for math/engineering C++.

                TIA

                WestCoast101
                The binary conversions should be above.
                Hex is simple.
                .ToString("X") on any integer will give you it's value as a hex string.
                Int.Parse(strin g); is overloaded with a 2nd value stateing if it should accept a hex string or not.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #23
                  Originally posted by Plater
                  The binary conversions should be above.
                  Hex is simple.
                  .ToString("X") on any integer will give you it's value as a hex string.
                  Int.Parse(strin g); is overloaded with a 2nd value stateing if it should accept a hex string or not.
                  Too bad this isn't C++.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #24
                    Originally posted by weaknessforcats
                    Too bad this isn't C++.
                    Ooops, didn't notice what forum I was in. (got this outa control pannel)

                    I am "borrowing" this code from an embeded development helpfile and modifying it. My C is a little rusty
                    [code=c]
                    BYTE FromHexHelper(c har digit)
                    {
                    if(digit<='9')
                    return(digit-'0');
                    else
                    return((toupper (digit)-'A')+10);
                    }

                    BYTE FromHex(char[2] hexdigit)
                    {
                    int lo,hi;

                    hi = FromHexHelper(h exdigit[0]);
                    lo = FromHexHelper(h exdigit[1]);
                    if(lo==0xdd)
                    return(hi);
                    else
                    return( hi*16+lo );
                    }

                    //pass in a char[2] (or bigger) array to hexdigit
                    void ToHex(byte val, char *hexdigit)
                    {
                    byte hi=0x00;
                    byte lo=0x00;

                    hi=0xF0&val;
                    lo=0x0F&val;
                    //you could do a switch statement on hi and lo to determine 0-9A-F
                    //then populate hexdigit[0] and hexdigit[1]
                    }
                    [/code]

                    Comment

                    • marulasiddesh
                      New Member
                      • Jul 2007
                      • 2

                      #25
                      Originally posted by saima
                      a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.

                      For example:
                      Enter a decimal number: 32
                      Its Binary Equivalent is : 000000100000
                      hi,
                      i am new to the forum can you suggest me how to send my queries and how to view the replies to my queries?

                      Comment

                      • Meetee
                        Recognized Expert Contributor
                        • Dec 2006
                        • 928

                        #26
                        Originally posted by marulasiddesh
                        hi,
                        i am new to the forum can you suggest me how to send my queries and how to view the replies to my queries?
                        Hi,

                        You kindly read Posting Guidelines

                        Regards

                        Comment

                        • HARICDAC
                          New Member
                          • Dec 2008
                          • 1

                          #27
                          C program for Converting decimal number to binary number

                          < spoonfeeding code deleted >

                          Comment

                          • donbock
                            Recognized Expert Top Contributor
                            • Mar 2008
                            • 2427

                            #28
                            You could repeatedly extract one nibble at a time from your value, using that nibble to index into an array of strings. The binary-string array contains 4-character strings such as "0000", "0001", etc. The hex-string array contains 1-character strings such as "0", "1", etc. Each such array has 16 entries.

                            How many nibbles are in the value? You either have to have secret knowledge of your compiler that you hard-code into your software or perhaps you could look at <limits.h> to see if your program can adapt itself to the compiler.

                            Comment

                            • weaknessforcats
                              Recognized Expert Expert
                              • Mar 2007
                              • 9214

                              #29
                              No one has mentioned an algorithm to solve this.

                              To check that a bit is set in a binary column, all you do is:

                              (32/32) = 1 and 1%2 is 1 the 32-bit is set
                              (32/16) = 2 and 2 % 2 is 0 the 16-bit is not set
                              etc...

                              Comment

                              • MrPickle
                                New Member
                                • Jul 2008
                                • 100

                                #30
                                Originally posted by weaknessforcats
                                No one has mentioned an algorithm to solve this.

                                To check that a bit is set in a binary column, all you do is:

                                (32/32) = 1 and 1%2 is 1 the 32-bit is set
                                (32/16) = 2 and 2 % 2 is 0 the 16-bit is not set
                                etc...
                                If you're using STL bitsets you can do myBitset.test(p osition);

                                Comment

                                Working...