views:

18

answers:

2

I'm trying to integrate two systems that deal with images. One system provides an image as a sbyte[] and the other uses a BitArray. I need to take the data from the sbyte[] and convert it into a BitArray. Anyone know how to do this?

Thanks, Paul

+1  A: 

The simplest way would be to convert the sbyte[] to a byte[] and then pass it into the normal BitArray constructor. If you're using .NET 3.5 that's easy with LINQ:

byte[] bytes = sbytes.Select(s => (byte) s).ToArray();
BitArray bitArray = new BitArray(bytes);

This is assuming you're executing in an unchecked context already. Otherwise you might want to make the conversion explicitly unchecked:

byte[] bytes = sbytes.Select(s => unchecked((byte) s)).ToArray();
BitArray bitArray = new BitArray(bytes);
Jon Skeet
Cool, so how do I convert sbyte[] to byte[]. Thanks.
Paul
@Paul: Did you read the whole of my answer? It shows you - twice!
Jon Skeet
O ya, duu.... thanks Jon - You Rock!
Paul
A: 

BitArray has a constructor that takes a byte array that you might try:

sbyte[] sbytes = ...
BitArray ba = new BitArray(sbytes.Select(x => (byte)x).ToArray());
Darin Dimitrov