Copy bits of Byte array to Int32

L

Lance

I have an array of bytes that I need to convert into an
array of Integers. But, the number of bits per value in
the Byte array is not necessarily divisible by 8 (although
it will never exceed 32).

For example, lets say I have a an array of bytes
(byteValues) that represents a string of 7 bit values. In
this case, the first value would be stored in the first 7
bits of byteValues(0), the second value would be stored in
the last bit of byteValues(0) and the first 6 bits of
byteValues(1), etc.

So, I need a method that will copy bits from byteValues to
an Integer and allows me to specify the offset (in bits)
and the number of bits to copy. I thought there was a
method that would do this type of thing, but now I can't
find it.

Thanks for any help!
Lance
 
T

Tom Shelton

I have an array of bytes that I need to convert into an
array of Integers. But, the number of bits per value in
the Byte array is not necessarily divisible by 8 (although
it will never exceed 32).

For example, lets say I have a an array of bytes
(byteValues) that represents a string of 7 bit values. In
this case, the first value would be stored in the first 7
bits of byteValues(0), the second value would be stored in
the last bit of byteValues(0) and the first 6 bits of
byteValues(1), etc.

So, I need a method that will copy bits from byteValues to
an Integer and allows me to specify the offset (in bits)
and the number of bits to copy. I thought there was a
method that would do this type of thing, but now I can't
find it.

Thanks for any help!
Lance

Would System.BitConverter help?
 
J

Jackson Davis [MSFT]

-----Original Message-----
I have an array of bytes that I need to convert into an
array of Integers. But, the number of bits per value in
the Byte array is not necessarily divisible by 8 (although
it will never exceed 32).

For example, lets say I have a an array of bytes
(byteValues) that represents a string of 7 bit values. In
this case, the first value would be stored in the first 7
bits of byteValues(0), the second value would be stored in
the last bit of byteValues(0) and the first 6 bits of
byteValues(1), etc.

So, I need a method that will copy bits from byteValues to
an Integer and allows me to specify the offset (in bits)
and the number of bits to copy. I thought there was a
method that would do this type of thing, but now I can't
find it.

Thanks for any help!
Lance
.

Lance,

I have never seen a method that doeswhat you ask, but it
would not be too hard to write the method yourself using
bit masks. Here is a function I wrote in C# that takes an
integer value and the starting and ending bit, and
extracts the sub-integer. (I didn't test it too deeply,
but it seems to work). You can use this to write a class
that keeps track of where you are in your array and what
bit offsets you are at.

// returns an sub-integer contained in an integer
// given the first bit number and the second bit number
// for instance, if val is the binary number: 0010
// firstBit is 1, and secondBit is 2,
// this returns the binary number 0010.
// if first bit it 2 and secondBit is 4 then this returns
// the binary number 0001
public static int GetNumber(int val, int firstBit, int
secondBit)
{
int mask = 0;

if (firstBit < 1 || firstBit > 32 ||
secondBit < 1 || secondBit > 32)
{
throw new System.ArgumentException();
}
if (secondBit < firstBit)
{
throw new System.ArgumentException();
}

for (int i = 0; i < secondBit; i++)
{
mask = mask << 1;
mask++;
}

mask = mask << (firstBit - 1);

val = val & mask;
val = val >> (firstBit - 1);

return val;
}

Hope that helps get you in the right direction.

Jackson Davis [MSFT]
 
J

Jackson Davis [MSFT]

-----Original Message-----
-----Original Message-----
I have an array of bytes that I need to convert into an
array of Integers. But, the number of bits per value in
the Byte array is not necessarily divisible by 8 (although
it will never exceed 32).

For example, lets say I have a an array of bytes
(byteValues) that represents a string of 7 bit values. In
this case, the first value would be stored in the first 7
bits of byteValues(0), the second value would be stored in
the last bit of byteValues(0) and the first 6 bits of
byteValues(1), etc.

So, I need a method that will copy bits from byteValues to
an Integer and allows me to specify the offset (in bits)
and the number of bits to copy. I thought there was a
method that would do this type of thing, but now I can't
find it.

Thanks for any help!
Lance
.

Lance,

I have never seen a method that doeswhat you ask, but it
would not be too hard to write the method yourself using
bit masks. Here is a function I wrote in C# that takes an
integer value and the starting and ending bit, and
extracts the sub-integer. (I didn't test it too deeply,
but it seems to work). You can use this to write a class
that keeps track of where you are in your array and what
bit offsets you are at.

// returns an sub-integer contained in an integer
// given the first bit number and the second bit number
// for instance, if val is the binary number: 0010
// firstBit is 1, and secondBit is 2,
// this returns the binary number 0010.
// if first bit it 2 and secondBit is 4 then this returns
// the binary number 0001
public static int GetNumber(int val, int firstBit, int
secondBit)
{
int mask = 0;

if (firstBit < 1 || firstBit > 32 ||
secondBit < 1 || secondBit > 32)
{
throw new System.ArgumentException();
}
if (secondBit < firstBit)
{
throw new System.ArgumentException();
}

for (int i = 0; i < secondBit; i++)
{
mask = mask << 1;
mask++;
}

mask = mask << (firstBit - 1);

val = val & mask;
val = val >> (firstBit - 1);

return val;
}

Hope that helps get you in the right direction.

Jackson Davis [MSFT]
--
This posting is provided "AS IS" with no warranties, and
confers no rights
Samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

.

Yes, it appears System.BitConverter will do the same thing
as I did above.
Jackson Davis [MSFT]
 
J

Jay B. Harlow [MVP - Outlook]

Lance,
In addition to the others comments. You can use System.Buffer to copy a byte
array to an integer array. However the array needs to be the correct size.

My concern with both System.Buffer & System.BitConverter is that they use
the full 8 bits of a byte, where as you have only 7 bits of a byte.

You could possible do either a pre process on the byte array or a post
process on the integer array to get a full 8 bits in a byte...

Not sure if System.Collections.BitArray would help with getting the required
bits needed out or not.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top