从字节读取位 [英] Reading bits from bytes

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

问题描述

大家好.

我有点麻烦.我需要从一个字节读取位,其想法是我从流中读取一个字节,而我必须从该字节中读取位.
一个简单的示例是我读取了一个字节"A",该字节将是字节"065".现在,我需要将其读取为"01000001",实际上我需要将其读取为"0","1","0","0","0","0","0","1"或其他任何与True或False无关的解释.

假设地,该代码将计算一个文件中有多少"1"(真).

只要可以读取数据中的任何字节,我就可以使用任何编码,因为我需要读取的数据是上帝只知道什么.

我已经尝试了几件事,但是没有一件事情能正常工作.
我只是无法深入了解它.我读字节没问题,但似乎无法完成位部分.

我将不胜感激任何代码示例.

Hi all.

I''m having a little trouble. I need to read bits from a byte, the idea is that I read a byte from a stream and the I have to read bits from that byte.
A simple example would be I read a byte "A" which would be byte "065". Now I need to read that as "01000001", well actually I need to read it as "0","1","0","0","0","0","0","1" or any other interpretation like True or False it''s irrelevant.

Hypothetically, the code would count how many "1"(Trues) are there in a file.

I can live with any encoding as long as I can read any byte in the data as the data I need to read is God only knows what.

I''ve tried a couple of things but none of them worked as it should.
I just can''t get to the bottom of it. I read bytes no problem but just can''t seem to get the bits part done.

I''d appreciate any code sample. Thanks.

推荐答案

已阅读有关BitWise操作的文章,以确定各个位的状态.那是做事的原始方式.
http://visualbasic.about.com/od/usingvbnet/a/bitops01.htm [ ^ ]

现在我们还提供了BitArray,例如;
Have a read of this article on BitWise operations to determine the state of individual bits. That was the original way of doing things.
http://visualbasic.about.com/od/usingvbnet/a/bitops01.htm[^]

We now also have BitArray available e.g.;
Dim x As Byte
x = &H65
Dim ba As New BitArray({x})
MsgBox("Bit Status; " + ba.Get(7).ToString + _
                    " " + ba.Get(6).ToString + _
                    " " + ba.Get(5).ToString + _
                    " " + ba.Get(4).ToString + _
                    " " + ba.Get(3).ToString + _
                    " " + ba.Get(2).ToString + _
                    " " + ba.Get(1).ToString + _
                    " " + ba.Get(0).ToString)


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

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