Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5707823
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: C/C++

2006-10-25 14:38:36

从一个二进制文件中读取出一个bit

 

方法一:使用位字段

 

#include

#include

using namespace std;

 

struct bin

{

       unsigned int s0:1;

       unsigned int s1:1;

       unsigned int s2:1;

       unsigned int s3:1;

       unsigned int s4:1;

       unsigned int s5:1;

       unsigned int s6:1;

       unsigned int s7:1;

};

int main()

{

       ofstream fout("file.dat", ios::binary);

       bin test;

 

       test.s0=0;

       test.s1=1;

       test.s2=0;

       test.s3=0;

       test.s4=1;

       test.s5=1;

       test.s6=1;

       test.s7=0;

 

       cout<

       fout.write((char *)&test, sizeof(test));

      

       return 0;

}

 

使用UltraEdit或者是WinHex查看。

奇怪,怎么file.dat的内容是

4个字节,我们想要写进去的是一个字节,问题出在哪里呢?

 

对!是struct bin出了问题,我们可以查看它的大小

cout<

原来我们定义的struct的大小是4,看来位字段struct的大小取决于其中长度最大的元素。那么,我们把每一个元素的类型都改为bool类型的话,struct的大小就是1个字节了。

#include

#include

using namespace std;

 

struct bin

{

       bool s0:1;

       bool s1:1;

       bool s2:1;

       bool s3:1;

       bool s4:1;

       bool s5:1;

       bool s6:1;

       bool s7:1;

};

 

int main()

{

       ofstream fout("file.dat", ios::binary);

       bin test;

 

       test.s0=0;

       test.s1=1;

       test.s2=0;

       test.s3=0;

       test.s4=1;

       test.s5=1;

       test.s6=1;

       test.s7=0;

 

       cout<

       fout.write((char *)&test, sizeof(test));

      

       return 0;

}

查看结果:

OK,写进去了一个字节。

 

读取也是一样的:

#include

#include

using namespace std;

 

 

struct bin

{

       bool s0:1;

       bool s1:1;

       bool s2:1;

       bool s3:1;

       bool s4:1;

       bool s5:1;

       bool s6:1;

       bool s7:1;

};

 

int main()

{

       ifstream fin("file.dat",ios::binary);

       bin test;

      

       fin.read((char*)&test,sizeof(test));

 

       cout<<"test.s7 = "<

       cout<<"test.s6 = "<

       cout<<"test.s5 = "<

       cout<<"test.s4 = "<

       cout<<"test.s3 = "<

       cout<<"test.s2 = "<

       cout<<"test.s1 = "<

       cout<<"test.s0 = "<

 

       return 0;

}

 

查看结果,跟前面写进去的内容完全一致。

 

方法二:采用移位和与、或操作实现。

void ByteToBit(bool *Out, const char *In, int bits)

{

     for(int i=0; i<bits; i++)

         Out[i] = (In[i/8]>>(i%8)) & 1;

     //Get the last of the Byte

}

void BitToByte(char *Out, const bool *In, int bits)

{

     memset(Out, 0, (bits+7)/8);

     for(int i=0; i<bits; i++)

         Out[i/8] |= In[i]<<(i%8);

}

 

注意:

在结构体中使用位字段时,要注意不要使用指针。

Compiler Error C2033 

Error Message

'identifier' : bit field cannot have indirection

The bit field was declared as a pointer, which is not allowed.

The following sample generates C2033:

// C2033.cpp

struct S {

   int *b : 1;  // C2033

};

Possible resolution:

// C2033b.cpp

// compile with: /c

struct S {

   int b : 1;

};

 

阅读(2339) | 评论(0) | 转发(0) |
0

上一篇:bmp文件头信息

下一篇:Winpcap的发包功能

给主人留下些什么吧!~~