how do I convert a BitArray into a byte array?

22:11 [ Roadrunne] Hi, how can I get back a Byte-Array from a BitArray?
22:25 [ Flav] Roadrunne: do you want 1 bit stored per byte or 8?

BitArray bitArray = new BitArray(new bool[] {
true, true, true, false, false, false, false, false, // 7
true, false, true, false, false, false, false, false, // 5
false, false, false, true, false, false, false, false, // 8
true // 1
});

1 bit per byte would be wasteful – you’d likely want to have a bool array instead, which is easy enough to do:
bool[] boolArray = new bool[bitArray.Length];
bitArray.CopyTo(boolArray, 0);

8 bits per byte makes more sense, and you just need to allocate based on a ceiling to make sure you can fit in the number of bits you have in the BitArray (since the bitArray in the general case may not be of length 0 mod 8).
byte[] byteArray = new byte[(int)Math.Ceiling((double)bitArray.Length / 8)];
bitArray.CopyTo(byteArray, 0);

You probably noticed the BitArray declared in a “little endian” type of style – this is just reflecting how BitArray converts each set of 8 bits into a byte by default.

BitArray.CopyTo can also target Int32’s if you want that as your target. For more information, see the msdn page for BitArray.CopyTo

Advertisements

Visual Studio Express editions – FREE IF YOU ACT NOW!

Visual Studio 2005 Express editions rock!

Ok, so it’s really free as long as you download/install by November 6, 2006 – they phrased it oddly with “free for the first year” which (too many) people will parse as “it will stop being free on {Nov. 6, 2006 _or_ 1 year after I install it} – neither of which are true – as long as you download/install it before Nov. 6, it’s free forever (see the pricing faq where they cover this more explicitly).

I’m hoping to get around to playing with all of them, but one thing I want to point out is something I think is very cool – when the first beta came out, the Express editions didn’t have ClickOnce support – one of the C# devs actually posted on his blog about how to get it to kind-of work in the Express edition. However, in beta2 (and the final bits) the ClickOnce support is built-in – even in the Express editions!

This is very cool, especially for those that may not have, for instance, an IIS server around – the ClickOnce support only needs HTTP – Apache (or any other http.1.1 server) works just fine as a base for publishing your applications.

For instance, in my home (at this point for legacy reasons more than anything else), my actual server box (the one serving this page you’re reading) is a linux box running apache 2.0 – the box I’m typing this on is Windows XP, though – now with the Express editions, I can develop quick little C# apps and use ClickOnce to have people run them off my server, with all the slick auto-updating and install-like-a-real-program support ClickOnce brings with it!

I really hope to see thousands upon thousands of people use this and make their own free little apps. yes, there’s the requirement that the client have the necessary framework installed (although ClickOnce, IIRC, will guide the user through that if they don’t have the necessary framework installed already), but especially for broadband users, that’s a pretty fast (and simple) thing 🙂

how do i convert a number (long, int, etc.) to a hex string?

In the Java (and, well, J#) world there is toHexString, but it’s not around in the BCL for C#/VB/etc. to use – so what’s available?

Well, if you’re good to go with big-endian byte ordering (and you should be, it reads easier!) then you can really just ToString with the hex format:

myLong.ToString(“X16”);
myInt.ToString(“X8”);

What if you wanted to print them out, but a byte at a time? Or, more generally, what if you wanted to print a byte array as a hex string? For getting them into byte arrays, BitConverter has GetBytes (much like String does) which gets most of your primitive types into a byte array: http://msdn2.microsoft.com/en-us/library/system.bitconverter.getbytes

Once you have a byte array that you want to spit out in the de-ad-be-ef kind of format, you only need to call BitConverter’s ToString methods, which are made for taking byte arrays:

http://msdn2.microsoft.com/en-us/library/3a733s97