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#
  4. Byte to bit array problem

Byte to bit array problem

Scheduled Pinned Locked Moved C#
helpdata-structuresquestion
6 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.
  • G Offline
    G Offline
    gwithey
    wrote on last edited by
    #1

    I am experienceing a problem breaking up three bytes of data into bit arrays and assigning a value to an enumeration as a result, code shown bellow:

    if (UpdateLEDAndBeepStatus != null)
    {
    BitArray byte0 = new BitArray(commandData.Data[0]);
    BitArray byte1 = new BitArray(commandData.Data[1]);
    BitArray byte2 = new BitArray(commandData.Data[2]);

                     enumLEDAndBuzzer\[\] LEDAndBuzz\_enumArray = new enumLEDAndBuzzer\[15\];
    
                     
    
                     // byte 0 bit 0
                     if (byte0\[0\] == true) //byte0.Count .Get(8-8)
                     {
                        LEDAndBuzz\_enumArray\[0\] = enumLEDAndBuzzer.BUZZER\_STATE\_ON;
                     }
                     else
                     {
                        LEDAndBuzz\_enumArray\[0\] = enumLEDAndBuzzer.BUZZER\_STATE\_OFF;
                     }
                     // byte 0 bit 1
                     if (byte0\[1\] == true) //byte0.Count
                     {
                        LEDAndBuzz\_enumArray\[1\] = enumLEDAndBuzzer.LED1\_MCUEnable\_ON;
                     }
                     else
                     {
                        LEDAndBuzz\_enumArray\[1\] = enumLEDAndBuzzer.LED1\_MCUEnable\_OFF;
                     }
    

    When the project gets to this stage the values in the bytes are "255". Should that not mean all 8 bits will be true? However my code never enters the true section of my if statement claiming "byte0[0]" is false. Either that or i am going about checking the bit values wrong, can anyone help? Thanx George :)

    H 1 Reply Last reply
    0
    • G gwithey

      I am experienceing a problem breaking up three bytes of data into bit arrays and assigning a value to an enumeration as a result, code shown bellow:

      if (UpdateLEDAndBeepStatus != null)
      {
      BitArray byte0 = new BitArray(commandData.Data[0]);
      BitArray byte1 = new BitArray(commandData.Data[1]);
      BitArray byte2 = new BitArray(commandData.Data[2]);

                       enumLEDAndBuzzer\[\] LEDAndBuzz\_enumArray = new enumLEDAndBuzzer\[15\];
      
                       
      
                       // byte 0 bit 0
                       if (byte0\[0\] == true) //byte0.Count .Get(8-8)
                       {
                          LEDAndBuzz\_enumArray\[0\] = enumLEDAndBuzzer.BUZZER\_STATE\_ON;
                       }
                       else
                       {
                          LEDAndBuzz\_enumArray\[0\] = enumLEDAndBuzzer.BUZZER\_STATE\_OFF;
                       }
                       // byte 0 bit 1
                       if (byte0\[1\] == true) //byte0.Count
                       {
                          LEDAndBuzz\_enumArray\[1\] = enumLEDAndBuzzer.LED1\_MCUEnable\_ON;
                       }
                       else
                       {
                          LEDAndBuzz\_enumArray\[1\] = enumLEDAndBuzzer.LED1\_MCUEnable\_OFF;
                       }
      

      When the project gets to this stage the values in the bytes are "255". Should that not mean all 8 bits will be true? However my code never enters the true section of my if statement claiming "byte0[0]" is false. Either that or i am going about checking the bit values wrong, can anyone help? Thanx George :)

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      Try:

      BitArray byte0 = new BitArray(new byte\[\] {commandData.Data\[0\]});
      

      I think that what your original line does is to create a BitArray of size 255, not one of size 8 containing 255.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      L G 2 Replies Last reply
      0
      • H Henry Minute

        Try:

        BitArray byte0 = new BitArray(new byte\[\] {commandData.Data\[0\]});
        

        I think that what your original line does is to create a BitArray of size 255, not one of size 8 containing 255.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        your thinking has been correct. I know so by reading the documentation, something everyone should do from time to time. :laugh:

        Luc Pattyn

        :badger: :jig: :badger:

        Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

        :jig: :badger: :jig:

        H 1 Reply Last reply
        0
        • L Luc Pattyn

          your thinking has been correct. I know so by reading the documentation, something everyone should do from time to time. :laugh:

          Luc Pattyn

          :badger: :jig: :badger:

          Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

          :jig: :badger: :jig:

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Luc Pattyn wrote:

          your thinking has been correct

          That's nice to know. I shall note this rare occurrence in my diary. :)

          Luc Pattyn wrote:

          I know so by reading the documentation, something everyone should do from time to time.

          So did I, but without trying, or seeing, all of the OPs code, I can never be sure.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          L 1 Reply Last reply
          0
          • H Henry Minute

            Luc Pattyn wrote:

            your thinking has been correct

            That's nice to know. I shall note this rare occurrence in my diary. :)

            Luc Pattyn wrote:

            I know so by reading the documentation, something everyone should do from time to time.

            So did I, but without trying, or seeing, all of the OPs code, I can never be sure.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Yeah I often regret the declarations aren't included in the code snippet.

            Henry Minute wrote:

            That's nice to know

            NP :)

            Luc Pattyn

            :badger: :jig: :badger:

            Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

            :jig: :badger: :jig:

            1 Reply Last reply
            0
            • H Henry Minute

              Try:

              BitArray byte0 = new BitArray(new byte\[\] {commandData.Data\[0\]});
              

              I think that what your original line does is to create a BitArray of size 255, not one of size 8 containing 255.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              G Offline
              G Offline
              gwithey
              wrote on last edited by
              #6

              Thank you it saved me alot of time trying to work it out. Did wonder why it said the length was 255 when running that was why.

              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