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 4 2937
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.BitConve rter help?
--
Tom Shelton
MVP [Visual Basic] -----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.Argument Exception();
}
if (secondBit < firstBit)
{
throw new System.Argument Exception();
}
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 -----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(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
7bits 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.Argument Exception(); } if (secondBit < firstBit) { throw new System.Argument Exception(); }
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.BitConve rter 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
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.BitConve rter 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.Collecti ons.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jeff |
last post by:
how do you convert form byte to Int32 while retaining the binary
value of the byte array
|
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 code '1101', what I should write? 13? no, I don't
think so
|
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 return an array as a property
because it will return a copy of the array instead a reference to it. How
can I force the property to return a reference...
|
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 store this Int32 array,
I do so as follows
Byte byteViewColWidths = (Byte)objViewColWidths;
m_nNumCols = byteViewColWidths.Length /...
|
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 char* m_pData;
//
| |
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;
|
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 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...
|
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 to show the array data to the end user.
Can I do that?
How?
|
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 better for me to use another
language (say vc++) and build a DLL to do the bit manipulating or does
vb.net have some way of working with bits.
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |