473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Copy bits of Byte array to Int32

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
Nov 20 '05 #1
4 2924
In article <03****************************@phx.gbl>, Lance wrote:
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?

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #2
-----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 (althoughit will never exceed 32).

For example, lets say I have a an array of bytes
(byteValues) that represents a string of 7 bit values. Inthis case, the first value would be stored in the first 7
bits of byteValues(0), the second value would be stored inthe 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 toan 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

Nov 20 '05 #3
-----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 7bits 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]--
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


Nov 20 '05 #4
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

"Lance" <zi***@hotmail.com> wrote in message
news:03****************************@phx.gbl...
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

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
8
by: Sowen | last post by:
Hi, all I am wondering how to write bits by using ofstream? I have finished a huffman tree, but how can I write the bits to the file in order to gain compression? for example, 'A' returns a...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
6
by: Brian Keating EI9FXB | last post by:
Hello there, I've just come accross this and wonder if i'm taking the best approach? I read a byte array from the registry, this byte array is basically an array of Int32 so i wish to revert and...
3
by: Dennis | last post by:
If I have a byte how would I port code from C++ to manipulate the byte array as if it were a long? For example: // C++ // assume the following // unsigned char* m_pPalette; // unsigned...
10
by: bg_ie | last post by:
Hi, I have an array as follows - char arr; Now I wish to copy the following int - int tmp = 0x01020304;
2
by: Pete | last post by:
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...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: s gall | last post by:
Hi all I have a task to test a checksum algrorithum. This requires changing a string of 12 chr's to a stream of 96 bits and then manipulating these. Currently I work in vb (vb2008). Would it be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.