简体   繁体   English

用C读取结构(结构信息)

[英]Reading Structures (Struct information) in C

My question is a bit strange. 我的问题有点奇怪。 I've given a Client-Server application, where in Client a Structure has been designed and it contains a Character array with size char values[1000] . 我提供了一个Client-Server应用程序,其中在Client中设计了一个Structure,它包含大小为char values[1000]的Character数组。 I will receive 1000 bits from Server in terms of 0's and 1's . 我将从0's and 1's角度从Server接收1000位。 In that I would like to read the bit from 885 and 886's position . 因此,我想读一下885 and 886's position How to execute this properly? 如何正确执行? Coz am getting strange characters when I read at that positions. 当我在那个位置阅读时,Coz正在变得奇怪。 I think it's because the bits are stored as mass data inside that values[] array like values[1000]={111010101....1} and not like normal array-> int abc[2] = {1,2}; 我认为是因为这些位以大容量数据的形式存储在values[]数组中,例如values[1000]={111010101....1}而不是普通的array-> int abc[2] = {1,2}; .

Is this the reason? 这是原因吗? Then how can I read 885 and 886's positions from values[1000]={111010101....1} ? 然后,如何从values[1000]={111010101....1}读取885 and 886's positions

Kindly clarify... 请澄清一下...

There seems to be a lot of confusion here. 这里似乎有很多混乱。

Assuming 8-bit char s just to keep the confusion down, bits 885 and 886 will be in character 110 (counting from 0, that's the 81st character). 假设8位char只是为了减少混乱,位885和886将位于字符110中(从0开始计数,即第81个字符)。

If we index the bits from the LSB as bit 0, then that character has these bits in it: 如果我们将来自LSB的位索引为位0,则该字符中包含以下位:

       88888888
bit nr 88888888
       76543210
        **

Reading vertically, we find 885 and 886 where indicated with asterisks ( * ). 垂直阅读,我们发现885和886带有星号( * )。

So, to extract these two bits: 因此,要提取这两个位:

const int bit885 = (values[110] & 0x20) >> 5;
const int bit886 = (values[110] & 0x40) >> 6;

Your char array contains 8 bits on each position as a char takes up one byte. 您的char数组在每个位置包含8位,因为char占用一个字节。 I suppose that you receive a byte stream from the server? 我想您从服务器收到字节流了吗? This means that each position takes up 8 bits and you will have to find the correct position by division, eg: 这意味着每个位置占用8位,并且您将必须通过除法找到正确的位置,例如:

char c = values[885/8];

After this still have to shift or mask the correct bit in those 8 bits. 之后,仍然必须对这8位中的正确位进行移位或屏蔽。 You could find the bit position by modulo division: 885 % 8 您可以通过模除找到位位置:885%8

Read up on bits and bytes and bit operations (and, or, shift) 读取位和字节以及位操作(以及或移位)

Also check out how your data is received on the server and how many bytes you receiv 还要检查如何在服务器上接收数据以及接收多少字节

Assuming that you store 8 bits per single char , it can be done this way. 假设每个char存储8位,则可以通过这种方式完成。 Its the way we use PIN register in ARM. 这是我们在ARM中使用PIN寄存器的方式。

int bitState = 0;
char buff[1000];

//Some execution that updates the buff

bitState = ((buff & (1 << 885)) ? 1 : 0);
if(bitState)
  //DO this;
else
  //DO that;

bitState = ((buff & (1 << 886)) ? 1 : 0);
if(bitState)
  //DO this;
else
  //DO that;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM