打包数据结构 - 如何定义? [英] packed datastructure - how to define?
问题描述
嗨
也许这是一个简单的问题,但我不知道如何解决。
背景:天气站连接到串口发送数据
包。这个数据包包含一个字节的变量,最多4个b / b
按混合顺序排列。现在我想定义一个结构,用接收缓冲区上的
UNION覆盖它,以便轻松访问
数据包中的单个值。
问题是,我无法定义一个变量,它只保留一个内存的b
字节。一个简单的''char''或''char [1]''占用4个字节,所有以下
变量都不匹配。
我怎么能定义一个变量,它只使用一个字节?
这是一个小文本应用程序:
----------------- -------------------------
#include< stdio.h>
# include< stdlib.h>
struct data {
int i;
char c;
int j;
};
union ovr {
struct data d;
char c [16];
};
int main(无效){
union ovr d;
int l;
for(l = 0; l< 16; l ++)
dc [l] = l;
printf(" size =%d \ n",sizeof(d));
printf(" i =%X \ n",ddi) ;
printf(" c =%X \ n",ddc);
printf(" j =%X \ n",ddj);
}
--------------------------------
和输出:
size = 16
i = 3020100
c = 4
j = B0A0908
覆盖数据包正确,j必须是8070605.
-
$ b $bJürgen
www.cfjh.de
推荐答案
您好Juergen,
JürgenHochwald写道:
Hi Juergen,
Jürgen Hochwald wrote:
也许这是一个简单的问题,但我不知道如何解决。
背景:连接到串口的气象站发送数据包。这些数据包包含以混合顺序排列的一个字节到4的变量。现在我想定义一个结构,用接收缓冲区上的UNION覆盖它,以便轻松访问
数据包中的单个值。
问题是,我无法定义变量,它只保留一个字节的内存。一个简单的''char''或''char [1]''占用4个字节,所有后面的
变量都不匹配。
如何定义一个变量,它只使用一个变量byte?
这是一个小文本应用程序:
----------------------------- -------------
#include< stdio.h>
#include< stdlib.h>
struct data {
int i;
char c;
int j;
};
union ovr {
struct data d;
char c [16];
};
int main(void){
union ovr d;
int l;
for(l = 0; l< 16; l ++)
dc [l] = l;
printf(" size =%d \ n",sizeof(d));
printf(i =%X \ n,ddi);
printf(" c =%X \ nn,ddc);
printf(" j =%X \\ \\ n,ddj);
}
--------------------------------
和输出:
size = 16
i = 3020100
c = 4
j = B0A0908
要正确覆盖数据包,j必须是8070605 。
Maybe this is a simple question, but I don''t know how to solve.
Background: A weather station connected to the serial port sends data
packets. This data packets are containing variables fom one byte up to 4
byted in mixed order. Now I want to define a structure to overlay it with
UNION over the receive buffer for easily access to the single values in the
data packet.
The problem is, that I cannot defind a variable, which only reserves one
byte of memory. A simple ''char'' or ''char[1]'' eats 4 bytes and all following
variables are not matching.
How can I define a variable, which uses exactly one byte ?
Here is a small text app:
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
struct data {
int i;
char c;
int j;
};
union ovr {
struct data d;
char c[16];
};
int main(void) {
union ovr d;
int l;
for (l=0;l<16;l++)
d.c[l]=l;
printf("size=%d\n",sizeof(d));
printf("i=%X\n",d.d.i);
printf("c=%X\n",d.d.c);
printf("j=%X\n",d.d.j);
}
--------------------------------
and the output:
size=16
i=3020100
c=4
j=B0A0908
To overlay the data packet correct, j must be 8070605.
这里有几个问题:
- 如果你想按字节顺序访问某些内容,请使用unsigned char
- char / signed char / unsigned char _always_的大小是一个
字节
- 一个字节不一定是8位但至少是这么多。
从< limits.h>中检查符号常量CHAR_BIT。找出
的确切比例
如果在< stdint.h>中有一个带整数类型的C99编译器,
那么你可以使用uint8_t类型。 8位通常被称为btet和btet。
- sizeof(int)> = sizeof(unsigned char),所以你的假设是关于
a某个比例可能是错误的
- 在一个结构中,如果有对齐要求,成员之间可能有填充字节
。如果sizeof(int)> 1和
整数必须是sizeof(int) - 对齐(也就是说,他们的地址
可以除以sizeof(int)而没有余数),然后就是
可能是你的结构中c和j
成员之间的sizeof(int)-1填充字节。
旁白:如果你有更大的结构然后它是有意义的
首先放置最大类型的变量然后降低尺寸 - 这通常导致结构更小。
- 大小> 1的类型的字节不一定按照你假设的b / b $ b来排序。因此,
unsigned int i = 1;
unsigned char lowestbyte =((unsigned char *)& i)[0];
可能会产生最低字节== 1或最低字节== 0。
所以,检查必要的尺寸,使用
union ovr {
struct data d;
unsigned char c [sizeof(struct data)];
};
或根本不使用union但是直接访问结构表示
,找出字节顺序(或者:使用移位和屏蔽
而不是使用unsigned char访问可移植代码),依此类推。
BTW:相对于结构的开始,offsetof宏会产生
a结构成员的偏移量。
comp.lang.c FAQ详细解决了其中的几个问题。
它已于11月1日发布,也可以在
http://www.eskimo.com/~scs/C-常见问题/的top.html
与HTML版本不同,ASCII版本是最新的。
HTH
Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
There are several issues here:
- If you want to access something bytewise, use unsigned char
- The size of a char/signed char/unsigned char _always_ is one
byte
- A byte is not necessarily eight bit but at least so much.
Check the symbolic constant CHAR_BIT from <limits.h> to find
out the exact ratio
If you have a C99 compiler with the integer types in <stdint.h>,
then you can use the type uint8_t. 8 Bits often are referred to
as "octet"
- sizeof(int)>=sizeof(unsigned char), so your assumptions about
a certain ratio may be wrong
- In a structure, there may be padding bytes between the members
if there are alignment requirements. If sizeof(int) > 1 and
ints have to be sizeof(int)-aligned (that is, their address
can be divided by sizeof(int) without remainder), then there
are probably sizeof(int)-1 padding bytes between the c and j
members of your structure.
As an aside: If you have larger structures then it makes sense
to put the variables with the largest type first and then go
down in size -- this often leads to smaller structures.
- The bytes of types with size >1 are not necessarily ordered as
you assume. Thus,
unsigned int i=1;
unsigned char lowestbyte=((unsigned char *)&i)[0];
may yield lowestbyte==1 or lowestbyte==0.
So, check the necessary size, use
union ovr {
struct data d;
unsigned char c[sizeof(struct data)];
};
or do not use a union at all but access the structure representation
directly, find out the byte order (or: rather use shifting and masking
instead of access by unsigned char for portable code), and so on.
BTW: The offsetof macro yields the offset in bytes of a member of
a structure with respect to the start of the structure.
The comp.lang.c FAQ addresses several of these issues in detail.
It has been posted here on November 1 and can also be found at
http://www.eskimo.com/~scs/C-faq/top.html
The ASCII version is up to date, unlike the HTML version.
HTH
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
=?ISO- 8859-15?Q?J = FCrgen?= Hochwald写道:
=?ISO-8859-15?Q?J=FCrgen?= Hochwald wrote:
问题是,我无法定义一个变量,它只保留
一个字节记忆一个简单的''char''或''char [1]''占用4个字节,所有
跟随变量不匹配。
不,它不是,或者它不是C编译器。是什么让你认为它是
占用超过一个字节?
结构数据{
int i;
char c;
int j;
};
union ovr {
struct data d;
char c [16];
};
int main(void){
union ovr d;
int l;
for(l = 0; l< 16; l ++)
dc [l] = l;
printf(" size =%d \ n",sizeof(d));
printf(" i =%X \\ \\ n",ddi);
printf(" c =%X \ n",ddc);
printf(" j =%X \ n",ddj);
}
--------------------------------
和输出:
size = 16
i = 3020100
c = 4
j = B0A0908
The problem is, that I cannot defind a variable, which only reserves one byte of memory. A simple ''char'' or ''char[1]'' eats 4 bytes and all following variables are not matching.
No it doesn''t, or it''s not a C compiler. What makes you think it''s
taking up more than one byte?
struct data {
int i;
char c;
int j;
};
union ovr {
struct data d;
char c[16];
};
int main(void) {
union ovr d;
int l;
for (l=0;l<16;l++)
d.c[l]=l;
printf("size=%d\n",sizeof(d));
printf("i=%X\n",d.d.i);
printf("c=%X\n",d.d.c);
printf("j=%X\n",d.d.j);
}
--------------------------------
and the output:
size=16
i=3020100
c=4
j=B0A0908
啊,数据大小不是问题,它是结构对齐。
ISO标准C中没有办法定义打包的结构。你的
实施可能会提供一种方式。
Brian
Ah, it isn''t the data size that is the problem, it''s struct alignment.
There is no way in ISO standard C to define packed structs. Your
implementation may provide a way.
Brian
2004年11月3日星期三22:45:19 + 0100,JürgenHochwald< jh@cfjh.de>
写道:
On Wed, 03 Nov 2004 22:45:19 +0100, Jürgen Hochwald <jh@cfjh.de>
wrote:
嗨
背景:连接到串口的气象站发送数据包。这些数据包包含以混合顺序排列的一个字节到4的变量。现在我想定义一个结构,用接收缓冲区上的UNION覆盖它,以便轻松访问
数据包中的单个值。
问题是,我无法定义变量,它只保留一个字节的内存。一个简单的''char''或''char [1]''占用4个字节,所有后面的
变量都不匹配。
如何定义一个变量,它只使用一个变量字节?
< snip>
结构数据{
int i;
char c;
int j;
};
< snip>
Hi
Maybe this is a simple question, but I don''t know how to solve.
Background: A weather station connected to the serial port sends data
packets. This data packets are containing variables fom one byte up to 4
byted in mixed order. Now I want to define a structure to overlay it with
UNION over the receive buffer for easily access to the single values in the
data packet.
The problem is, that I cannot defind a variable, which only reserves one
byte of memory. A simple ''char'' or ''char[1]'' eats 4 bytes and all following
variables are not matching.
How can I define a variable, which uses exactly one byte ?
<snip>
struct data {
int i;
char c;
int j;
};
<snip>
您在主题行中有答案,但
结构中的数据打包是实现和平台依赖和关闭主题
在这里。您的编译器可能有一个开关或编译指示强制要打包结构
。 OTOH,您的平台可能不允许在奇数
边界上访问j。您可以考虑替代方案,例如定义一个int和
将字节5-8复制到其中。不是一个强大的,便携的方法,但它可能会工作。$ / b
-
Al Balmer
Balmer Consulting
重新** **********************@att.net
这篇关于打包数据结构 - 如何定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!