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?
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);
sbyte sbytes = ... BitArray ba = new BitArray(sbytes.Select(x => (byte)x).ToArray());