开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜