从字节数组中获取特定位 [英] Get a specific bit from a byte array

查看:54
本文介绍了从字节数组中获取特定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从字节数组中获取特定位。例如37 [2]。



我有HistoryElements [37]但是在这里我必须显示第37个字节的第2位。



我尝试了什么:



我使用过Bitconverter.GetBytes和Bitarray,但它有效。

How to get a specific bit from a byte array.For example 37[2].

I have HistoryElements[37] but here i have to display the 2nd bit of 37th byte.

What I have tried:

I have used Bitconverter.GetBytes and Bitarray but it dint worked.

推荐答案

使用 BitArray 类。例如

Use BitArray class. e.g.
byte[] b = new byte[37];
     for (int n = 0; n < b.Length; ++n)
       b[n] = (byte)n;

     // get bit 5 from 37th item
     System.Collections.BitArray ba = new System.Collections.BitArray(new byte[1]{b[36]});

     Console.WriteLine("item[36][5] is {0}", ba.Get(5));


这篇关于从字节数组中获取特定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆