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. Convert To Binary

Convert To Binary

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmstutorial
7 Posts 4 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.
  • S Offline
    S Offline
    Static x
    wrote on last edited by
    #1

    I have written class inherited from CString for doing arithmetic operations with huge numbers,but it takes too long to convert a decimal number to binary using standard algorithm(dividing on 2,...). situation is like this: i want to convert (for example) CString g = "134.......45645675"; to Binary //CString binary; //binary=SOMEFUNCTION(g); binary = "10000100...10011" can you give me any link where i can find information about converting from decimal to binary using shift left and shift right operaations,or give me any tipps,i think using these operations will make conversion faster. Thanks in advance m0n0

    D T 2 Replies Last reply
    0
    • S Static x

      I have written class inherited from CString for doing arithmetic operations with huge numbers,but it takes too long to convert a decimal number to binary using standard algorithm(dividing on 2,...). situation is like this: i want to convert (for example) CString g = "134.......45645675"; to Binary //CString binary; //binary=SOMEFUNCTION(g); binary = "10000100...10011" can you give me any link where i can find information about converting from decimal to binary using shift left and shift right operaations,or give me any tipps,i think using these operations will make conversion faster. Thanks in advance m0n0

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

      Giorgi Moniava wrote: can you give me any link where i can find information about converting from decimal to binary I found this in my toolbox. It's old so adjust as necessary:

      void DWORD_To_BinaryString( DWORD value,char string[] )
      {
      for (int bit = 0; bit < 32; bit++)
      {
      DWORD mask = 0x80000000 >> bit;

          if ((value & mask) == mask) 
              string\[bit\] = '1';        
          else                        
              string\[bit\] = '0';    
      }    
      
      string\[32\] = '\\0';
      

      }


      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

      L 1 Reply Last reply
      0
      • S Static x

        I have written class inherited from CString for doing arithmetic operations with huge numbers,but it takes too long to convert a decimal number to binary using standard algorithm(dividing on 2,...). situation is like this: i want to convert (for example) CString g = "134.......45645675"; to Binary //CString binary; //binary=SOMEFUNCTION(g); binary = "10000100...10011" can you give me any link where i can find information about converting from decimal to binary using shift left and shift right operaations,or give me any tipps,i think using these operations will make conversion faster. Thanks in advance m0n0

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

        well, i don't know any way to convert a string with an integer long enough into another "binary" string. if you work directly on an integer, you can do this :

        int iVal = 125932; // For example
        CString strVal = ""; // CString because more easy to use

        while (/*...*/) {
        strVal.Insert(0, ((iVal & 0x00000001) ? "1" : "0"));
        iVal >>= 1;
        }

        BUT ! if you have a string, unless you cast its content into a binary (short, int or long) type, this method is not applyable beacause of the risk of bits lack (overflow). do you already have some shorst ideas on the subject to share ? [EDIT] ...And as Mr Giorgi Moniava asked to Mr Prakash, i'll try to explain a bit more my code (which is quite similar to Mr Prakash's). - with CString::Insert(0, ...), I add at position 0 the second parameter (i hope i place them in the right order). - with (iVal & 0x00000001), I use the Bitwise-AND operation to get if the lower bit is set at 1 or 0. In fact, I should have written ((iVal & 0x00000001) == 0x00000001). this operation does this :

        iVal -> 100010101101011010010100001010011
             **&**  000000000000000000000000000000001
              -------------------------------------
             == 000000000000000000000000000000001
        
        iVal -> 100010101101011010010100001010100
             **&**  000000000000000000000000000000001
              -------------------------------------
             == 000000000000000000000000000000000
        

        that's what Mr Prakash does with its mask. - if you know about the C/C++ operator ?:, you also know that if its 1st parameter (the condition) is true, then the 2nd parameter is returned, otherwise, it is the 3rd parameter to be returned. This way, the condition is true if the LSB (Lower Significant Bit) is set to 1. So, i insert "1" in the string. - Then, i shift all the bits to the right and test again what i've just done before... [/EDIT]


        TOXCCT >>> GEII power
        [toxcct][

        L 1 Reply Last reply
        0
        • D David Crow

          Giorgi Moniava wrote: can you give me any link where i can find information about converting from decimal to binary I found this in my toolbox. It's old so adjust as necessary:

          void DWORD_To_BinaryString( DWORD value,char string[] )
          {
          for (int bit = 0; bit < 32; bit++)
          {
          DWORD mask = 0x80000000 >> bit;

              if ((value & mask) == mask) 
                  string\[bit\] = '1';        
              else                        
                  string\[bit\] = '0';    
          }    
          
          string\[32\] = '\\0';
          

          }


          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Thanks David for your attention, but i need function like CString string_To_BinaryString( CString value ) { CString result; ... ... return result; } you know actually variable 'value' in my program appears to be huge number example : "72389472389472389....234234234234" it maybe 300 digit or more.thats why standard algorithm(division by 2,...) is very slow.you know i am a little inexperienced programmer so things like '0x80000000' are not similiar to me , i would be very thankful to you if you could give me a little explanation about how to adjust your function in order to get what i need. Thanks in advance m0n0

          D 1 Reply Last reply
          0
          • L Lost User

            Thanks David for your attention, but i need function like CString string_To_BinaryString( CString value ) { CString result; ... ... return result; } you know actually variable 'value' in my program appears to be huge number example : "72389472389472389....234234234234" it maybe 300 digit or more.thats why standard algorithm(division by 2,...) is very slow.you know i am a little inexperienced programmer so things like '0x80000000' are not similiar to me , i would be very thankful to you if you could give me a little explanation about how to adjust your function in order to get what i need. Thanks in advance m0n0

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

            I did a program like this before about 14 years ago. There was no limit to the length of the operands and the supported operators were +, -, *, /, ^, and !. I did not have to convert anything to binary, though.


            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            1 Reply Last reply
            0
            • T toxcct

              well, i don't know any way to convert a string with an integer long enough into another "binary" string. if you work directly on an integer, you can do this :

              int iVal = 125932; // For example
              CString strVal = ""; // CString because more easy to use

              while (/*...*/) {
              strVal.Insert(0, ((iVal & 0x00000001) ? "1" : "0"));
              iVal >>= 1;
              }

              BUT ! if you have a string, unless you cast its content into a binary (short, int or long) type, this method is not applyable beacause of the risk of bits lack (overflow). do you already have some shorst ideas on the subject to share ? [EDIT] ...And as Mr Giorgi Moniava asked to Mr Prakash, i'll try to explain a bit more my code (which is quite similar to Mr Prakash's). - with CString::Insert(0, ...), I add at position 0 the second parameter (i hope i place them in the right order). - with (iVal & 0x00000001), I use the Bitwise-AND operation to get if the lower bit is set at 1 or 0. In fact, I should have written ((iVal & 0x00000001) == 0x00000001). this operation does this :

              iVal -> 100010101101011010010100001010011
                   **&**  000000000000000000000000000000001
                    -------------------------------------
                   == 000000000000000000000000000000001
              
              iVal -> 100010101101011010010100001010100
                   **&**  000000000000000000000000000000001
                    -------------------------------------
                   == 000000000000000000000000000000000
              

              that's what Mr Prakash does with its mask. - if you know about the C/C++ operator ?:, you also know that if its 1st parameter (the condition) is true, then the 2nd parameter is returned, otherwise, it is the 3rd parameter to be returned. This way, the condition is true if the LSB (Lower Significant Bit) is set to 1. So, i insert "1" in the string. - Then, i shift all the bits to the right and test again what i've just done before... [/EDIT]


              TOXCCT >>> GEII power
              [toxcct][

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              i copied this code to my program and it works ideally to my surprise ,because i am unfamiliar to things like "0x00000001" , but unfortunately i need to cenvert Strings to Binary Strings ex : "17"DEC="10001"BIN and they appear to be very long(100 or more digits), anyway i'd be very thankful to you if you could give me a little explanation on how your code works, THanks in advance m0n0

              T 1 Reply Last reply
              0
              • L Lost User

                i copied this code to my program and it works ideally to my surprise ,because i am unfamiliar to things like "0x00000001" , but unfortunately i need to cenvert Strings to Binary Strings ex : "17"DEC="10001"BIN and they appear to be very long(100 or more digits), anyway i'd be very thankful to you if you could give me a little explanation on how your code works, THanks in advance m0n0

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

                Giorgi Moniava wrote: anyway i'd be very thankful to you if you could give me a little explanation on how your code works, certainly.... read again my previous post. i've added some explainations about my code at its end... cheers,


                TOXCCT >>> GEII power
                [toxcct][VisualCalc]

                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