C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 308,428 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 3,221 people online right now. Registration is fast and FREE... Join Now!




Convert BitArray to byte

 

Convert BitArray to byte

Kabanga

5 Nov, 2009 - 10:42 PM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2009
Posts: 16

Hi everyone!

I have a BitArray defined as follow:

CODE

//create an array of 256 boolean elements
bool[] myStates = new bool[256];

//create BitArray from the array myStates
BitArray myNewStates = new BitArray(myStates);



and an array of bytes defined like that:
CODE

//create an array of 65 bytes
Byte[] myBuffer = new byte[65];

//initialize the first 5 bytes
myBuffer[0] = 0x00;
myBuffer[1] = 0x01;
myBuffer[2] = 0x02;
myBuffer[3] = 0x03;
myBuffer[4] = 0x04;

//initialize array 38 to 65
for(int i=38; i<65; i++)
{
  myBuffer[i] = oxFF;
}



Now I'd like to populate bytes 6 to 37 (a total of 32 bytes) of myBuffer array using the BitArray myNewStates.
Since 256 bits (true, false states) is equivalent to 32 bytes, how to convert these boolean states into bytes?

Best regards
Kabanga

User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 5)

djkitt

RE: Convert BitArray To Byte

6 Nov, 2009 - 07:32 AM
Post #2

D.I.C Head
**

Joined: 22 May, 2008
Posts: 174



Thanked: 24 times
My Contributions
So,

You could try the CopyTo method...

CODE

     myNewStates.CopyTo(myBuffer, 6);



Hope this helps,

Kitt
User is offlineProfile CardPM
+Quote Post

Momerath

RE: Convert BitArray To Byte

6 Nov, 2009 - 09:18 AM
Post #3

D.I.C Addict
****

Joined: 4 Oct, 2009
Posts: 509



Thanked: 52 times
My Contributions
Depends on how you want to copy the bits over. For example in an 8 position BitArray, they are in this order 76543210. But it's possible that you want them in the Bytes in order from low to high, or in reverse order (where the last byte in the byte list is the lowest indexes).

User is online!Profile CardPM
+Quote Post

Kabanga

RE: Convert BitArray To Byte

6 Nov, 2009 - 11:12 PM
Post #4

New D.I.C Head
*

Joined: 5 Mar, 2009
Posts: 16

thanks guys, it was helpful!

@ Momerath
yeah, your are right: I'd like them to be in the order 01234567. How to do that?.
In the Bytes I'd like the last byte in the list have the highest index.

Best regards
Kabanga
User is offlineProfile CardPM
+Quote Post

Momerath

RE: Convert BitArray To Byte

6 Nov, 2009 - 11:30 PM
Post #5

D.I.C Addict
****

Joined: 4 Oct, 2009
Posts: 509



Thanked: 52 times
My Contributions
csharp
public byte[] BitArrayToByteArray(BitArray ba) {
byte[] bytes;

if (ba.Length % 8 == 0) {
bytes = new byte[ba.Length / 8];
} else {
bytes = new byte[ba.Length / 8 + 1];
}

for (int i = 0; i < ba.Length; i += 8) {
int pos = i / 8;

for (int j = 0; j < 8; j++) {
if (i + j < ba.Length) {
if (ba[i + j] == true) {
bytes[pos] &= 1;
}
}
bytes[pos] <<= 1;
}
}

return bytes;
}


This is off the top of my head, I can already see some ways to improve it smile.gif

This post has been edited by Momerath: 6 Nov, 2009 - 11:33 PM
User is online!Profile CardPM
+Quote Post

Kabanga

RE: Convert BitArray To Byte

7 Nov, 2009 - 11:07 AM
Post #6

New D.I.C Head
*

Joined: 5 Mar, 2009
Posts: 16

thanks. I'll give it a try!
Which improvements do you mean?

Best regards
Kabanga

This post has been edited by Kabanga: 7 Nov, 2009 - 11:09 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/24/09 01:47PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month