【高手求助】小字节序转为大字节序函数

Barbiegirl2010 2012-09-17 03:03:29
昨天的一道笔试题,编写如下结构的小字节序转大字节序的转换函数:void ConvertLToB(T_Sample*ptIn),结构的定义如下

#pragma pack(4)
typedef struct tagT_Sample{
BYTE ucA :3;
BYTE ucB :3;
BYTE ucC :3;
BYTE ucD :3;
BYTE ucE :3;
DWORD dwF :24;
}T_Sample;

自己对大小端也有点了解,但是也只是限于判断和求个输出什么的,哎,怪自己学艺不精,求高手来解答!
...全文
365 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Barbiegirl2010 2012-09-17
  • 打赏
  • 举报
回复
有人帮助小弟解决下么。。。
Barbiegirl2010 2012-09-17
  • 打赏
  • 举报
回复
嗯,是vc里面的。。。人家写了估计就得按他的来吧。。。好苦逼啊[Quote=引用 6 楼 的回复:]
C标准里没有#pragma pack吧!
[/Quote]
mymtom 2012-09-17
  • 打赏
  • 举报
回复
C标准里没有#pragma pack吧!
Barbiegirl2010 2012-09-17
  • 打赏
  • 举报
回复
#pragma pack(4)
这个不是说了按四字节对齐么,可以求出大小的么
[Quote=引用 3 楼 的回复:]
出题的就是个半桶水!

按照标准sizeof(T_Sample)都是不确定的!

对于位字段来说,几乎所有的描述都是不确定的implementation-defined或者unspecified.

A bit-field may have type int, unsigned int, or signed int. Whether the high-order bit positio……
[/Quote]
Barbiegirl2010 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
#pragma pack(4)
typedef struct tagT_Sample{
////1字节//////
BYTE ucA :3;
BYTE ucB :3;
//////////////

////1字节/////
BYTE ucC :3;
BYTE ucD :3;
//////////////

///1字节//////
BYTE ucE :3;
/////……
[/Quote]我也不清楚额。。。题目我拍照原封不动的抄过来的额,不过网上相关的都进行移位操作什么的,没看明白额
mymtom 2012-09-17
  • 打赏
  • 举报
回复
出题的就是个半桶水!

按照标准sizeof(T_Sample)都是不确定的!

对于位字段来说,几乎所有的描述都是不确定的[B]implementation-defined[/B]或者[B]unspecified[/B].

A bit-field may have type int, unsigned int, or signed int. Whether the high-order bit position of a “plain” int bit-field is treated as a sign bit is [B]implementation-defined[/B]. A bit-field is interpreted as an integral type consisting of the specified number of bits.

An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is [B]implementation-defined[/B]. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is [B]implementation-defined[/B]. The alignment of the addressable storage unit is [B]unspecified[/B].
qq120848369 2012-09-17
  • 打赏
  • 举报
回复
#pragma pack(4)
typedef struct tagT_Sample{
////1字节//////
BYTE ucA :3;
BYTE ucB :3;
//////////////

////1字节/////
BYTE ucC :3;
BYTE ucD :3;
//////////////

///1字节//////
BYTE ucE :3;
//////////////

//填充1字节///
/////////////

/////4字节////
DWORD dwF :24;
/////////////

}T_Sample;


就这么8个字节, SB面试官是打算让你把整个结构体的字节颠倒一下还是把DWORD字节的字节序颠倒一下(唯一的多字节),还是把每个域里的位颠倒一下?
Barbiegirl2010 2012-09-17
  • 打赏
  • 举报
回复
顶一下!求高手相助!

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧