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. Parse a binary number

Parse a binary number

Scheduled Pinned Locked Moved C / C++ / MFC
9 Posts 3 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.
  • N Offline
    N Offline
    NewHSKid
    wrote on last edited by
    #1

    Hi all, I was wondering if there was a way to parse a binary number that I receive over a TCP port, into either 4 bit chunks or into hex. Thank you. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

    D A 2 Replies Last reply
    0
    • N NewHSKid

      Hi all, I was wondering if there was a way to parse a binary number that I receive over a TCP port, into either 4 bit chunks or into hex. Thank you. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

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

      DWORD dwSome32BitNumber;

      WORD wFirstChunk = dwSome32BitNumber & 0x0000000f;
      WORD wSecondChunk = (dwSome32BitNumber >> 8) & 0x0000000f;
      WORD wThirdChunk = (dwSome32BitNumber >> 16) & 0x0000000f;
      WORD wFourthChunk = (dwSome32BitNumber >> 32) & 0x0000000f;
      ...

      Does that make sense? Or you could dress it up a bit with:

      WORD wChunks[8];
      for (int nIndex = 0; nIndex< 8; nIndex++)
      {
      wChunks[nIndex] = dwSome32BitNumber & 0x0000000f;
      dwSome32BitNumber >>= 8;
      }


      Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

      N 1 Reply Last reply
      0
      • N NewHSKid

        Hi all, I was wondering if there was a way to parse a binary number that I receive over a TCP port, into either 4 bit chunks or into hex. Thank you. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

        A Offline
        A Offline
        ASchunk
        wrote on last edited by
        #3

        Hi, yes you can split of the TCP address into its compounds and parse the compounds instead. Regards. Alex

        1 Reply Last reply
        0
        • D David Crow

          DWORD dwSome32BitNumber;

          WORD wFirstChunk = dwSome32BitNumber & 0x0000000f;
          WORD wSecondChunk = (dwSome32BitNumber >> 8) & 0x0000000f;
          WORD wThirdChunk = (dwSome32BitNumber >> 16) & 0x0000000f;
          WORD wFourthChunk = (dwSome32BitNumber >> 32) & 0x0000000f;
          ...

          Does that make sense? Or you could dress it up a bit with:

          WORD wChunks[8];
          for (int nIndex = 0; nIndex< 8; nIndex++)
          {
          wChunks[nIndex] = dwSome32BitNumber & 0x0000000f;
          dwSome32BitNumber >>= 8;
          }


          Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

          N Offline
          N Offline
          NewHSKid
          wrote on last edited by
          #4

          Hi David, Can you explain this to me a little more. I think I understand , but I am not exactly sure what the dwSome32BitNumber >> 8 (16,32) or dwSome32BitNumber >>= 8 does exactly with respect to the &0x00000000f. thanks again for your help Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

          D 1 Reply Last reply
          0
          • N NewHSKid

            Hi David, Can you explain this to me a little more. I think I understand , but I am not exactly sure what the dwSome32BitNumber >> 8 (16,32) or dwSome32BitNumber >>= 8 does exactly with respect to the &0x00000000f. thanks again for your help Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

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

            >> and << are right and left bit-shift operators, respectively. Read about them here: http://tinyurl.com/o94p


            Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

            N 1 Reply Last reply
            0
            • D David Crow

              >> and << are right and left bit-shift operators, respectively. Read about them here: http://tinyurl.com/o94p


              Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

              N Offline
              N Offline
              NewHSKid
              wrote on last edited by
              #6

              I'm sorry, I posted my message incomplete. I updated the previous post. My question basically is if you are you the bitwise & operator with the first 8bits of the number, won't it end up being 0? So i guess my real question is what does the & 0x00000000f do after the number has been shifted 8 (16, 32)? thank you Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

              D 1 Reply Last reply
              0
              • N NewHSKid

                I'm sorry, I posted my message incomplete. I updated the previous post. My question basically is if you are you the bitwise & operator with the first 8bits of the number, won't it end up being 0? So i guess my real question is what does the & 0x00000000f do after the number has been shifted 8 (16, 32)? thank you Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

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

                NewHSKid wrote: ...what does the & 0x00000000f do... Read about the bitwise-AND operator here: http://tinyurl.com/o96t


                Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                N 1 Reply Last reply
                0
                • D David Crow

                  NewHSKid wrote: ...what does the & 0x00000000f do... Read about the bitwise-AND operator here: http://tinyurl.com/o96t


                  Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                  N Offline
                  N Offline
                  NewHSKid
                  wrote on last edited by
                  #8

                  Hi David, I know what the bitwise AND does, thats why i don't understand what you are saying. If I AND all 4 of the word chunks with 0x00000000f, then won't they all just evaluate to 0x00000000f because there is no 1's to match up to? Please correct me, I have a feeling I am not thinking about this correctly. Lets say I get the number as 0xA012B435. Using the bitwise AND will just make it 0. No? Thanks for your help. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

                  D 1 Reply Last reply
                  0
                  • N NewHSKid

                    Hi David, I know what the bitwise AND does, thats why i don't understand what you are saying. If I AND all 4 of the word chunks with 0x00000000f, then won't they all just evaluate to 0x00000000f because there is no 1's to match up to? Please correct me, I have a feeling I am not thinking about this correctly. Lets say I get the number as 0xA012B435. Using the bitwise AND will just make it 0. No? Thanks for your help. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

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

                    AND means that BOTH bits must be 1 in order for the resulting bit to be 1.

                    10100000000100101011010000110101
                    AND 1111

                                             101
                    

                    Continuing to bit-shift the value to the right one word at a time means that you can use 0x0000000f exclusively. You could also have had something like: DWORD dw = 0xA012B435; WORD w1 = dw & 0x0000000f; WORD w2 = (dw & 0x000000f0) >> 8; WORD w3 = (dw & 0x00000f00) >> 16; WORD w4 = (dw & 000000f000) >> 24; or WORD w4 = dw & 000000f000; w4 >>= 24; Whether you bit-shift before or after the AND operation is not important, but doing so beforehand does lend itself to more readable code.


                    Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                    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