How to covert a sbyte[] to BitArray? C#.Net

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


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);

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());

Need Your Help

loading data into dataGridView from C structures

c# datagridview structure

I am trying to create a data visualizer in c# for an array of structures created in a C legacy app. I believe that the best way to do this is to use a winforms dataGridView. I am unable to find any

redirect output to terminal

bash echo stdout stderr io-redirection

I am trying to understand redirecting the output.