RE: Need Help getting values from bits.



Hey Pete,

You probably need to use bitmasking - in the C languages the bitwise
operators & (AND), | (OR), ~ (NOT), and ^ (XOR). See C# docs and
http://en.wikipedia.org/wiki/Bitwise_operators

If you have only 3 variables I'd be inclined to declare 3 boolean variables
and set them by masking the appropriate byte (untested):

byte[] bytes = { 1, 2 };

bool a = (bytes[0] & 0xFF) == 1; // TRUE because the first bit of bytes[0]
is ON
bool b = (bytes[0] & 0xFF) == 2; // FALSE because the 2nd bit of bytes[0] is
OFF
bool c = (bytes[1] & 0xFF) == 2; // TRUE because the 2nd bit of bytes[1] is ON
// Use the bool variables now...

If there's a bunch of significant bytes you might just test each one as you
go...

if ((bytes[1] & 0xFF) == 2) // do something

.... or maybe each byte represents only one condition? ...

if ((bytes[0] & 0xFF) > 0) // do something

HTH ... - KH


"Pete" wrote:

Hi,
First, thanks for any time you spend helping me, I'm at a loss. I'm not
bit-savvy, so I apologize if this is extremely simple, or I am going about
this the wrong way.

I am trying to take a byte array and extract some information from that
array, and convert it back to a hex value. For instance, I have a byte[]
that I populated from a BinaryReader of a file. Now, lets say it's 4 bytes
in length. Out of those 32 bits I need to get the hex value of certain
bits, like 3 bits starting at offset 10. The header information I am
reading from the file is not byte aligned, so most of the values I am going
to be looking at will be two to eight bits spanning different bytes.

I've been able to move the data into a BitArray, but from that point I'm at
a loss as to what to do with the BitArray, or if that is even the right
direction to go.

Any help in the right direction would be greatly appreciated.

Thank you!



.



Relevant Pages

  • Need Help getting values from bits.
    ... First, thanks for any time you spend helping me, I'm at a loss. ... Out of those 32 bits I need to get the hex value of certain ... The header information I am ... I've been able to move the data into a BitArray, but from that point I'm at ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Big endian convention in Ruby
    ... if the array holding this is ... messageHex[0].hex.to_i this will ensure that it is a integer in hex. ... Unpack directly to Integers, as shown above. ... It works perfectly fine with in give the loop 64 times.. ...
    (comp.lang.ruby)
  • RE: Newbie: BitArray initilization
    ... to 3 (to init a 10x10 array) produces a run time error. ... public class Test_compile_cls ... > and the 'object reference not set' error occurs on statement after the above code. ... So you can use various BitArray constructors and initialize the array elements before using them. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: byte data manipulation
    ... Read in the input hex file, stripping of the preamble and checksum ... and putting the bytes (2 chars) into array_1. ... explains why you need the array. ...
    (comp.lang.c)
  • Re: Hex to Binary Conversion
    ... I have an array of hex bytes. ... The conversion itself depends on exactly what you mean, as well as in what order the bytes are provided. ... complement of it ...
    (microsoft.public.dotnet.languages.csharp)