一个简单问题(50分)

  • 主题发起人 主题发起人 zzhdi
  • 开始时间 开始时间
Z

zzhdi

Unregistered / Unconfirmed
GUEST, unregistred user!
struct P_BUFFER
{
ULONG AId;
union
{
struct
{
USHORT TmpCode : 6;
USHORT Reserved : 10;
};
struct
{
USHORT Fin : 1;
USHORT Syn : 1;
USHORT Action : 8;
};
};
ULONG Time;
ULONG SourceDate;
ULONG DestinationData;

union
{
ULONG Id;
struct
{
ULONG aPort: 16;
ULONG bPort : 16;
};
};

ULONG DataBytes;
};
这样的C代码如何转换为pascal 代码
 
将 struct 声明为 record
先要定义子 struct 的类型
 
to jsxjd:
ULONG aPort: 16;
ULONG bPort : 16;
中的 16 是什么意思
 

ULONG aPort: 16;
ULONG bPort : 16;

这是C语言中的“位域”类型,代表aPort占16bit,bPort占16bit(一个ULONG为32 bit)

union
{
ULONG Id;
struct
{
ULONG aPort: 16;
ULONG bPort : 16;
};
};
这是一个联合体。找本C语言的书看一下就明白了。

位域在Pascal中不知道怎么表示,如果实在不行只能通过移位和相与实现。

联合体,也许可以参考下面的:
sockaddr_in = record
case Integer of
0: (sin_family: u_short;
sin_port: u_short;
sin_addr: TInAddr;
sin_zero: array[0..7] of Char);
1: (sa_family: u_short;
sa_data: array[0..13] of Char)
end;
TSockAddrIn = sockaddr_in;

 
C中的位域在Pascal中相当于下面例子:
type
TEmployee = record
FirstName, LastName: string[40];
BirthDate: TDate;
case Salaried: Boolean of
True: (AnnualSalary: Currency);
False: (HourlyWage: Currency);
end;
再例如
type
TPerson = record
FirstName, LastName: string[40];
BirthDate: TDate;
case Citizen: Boolean of
True: (Birthplace: string[40]);
False: (Country: string[20];
EntryPort: string[20];
EntryDate, ExitDate: TDate);
end;
type
TShapeList = (Rectangle, Triangle, Circle, Ellipse, Other);
TFigure = record
case TShapeList of//Case后面必须为序数类型
Rectangle: (Height, Width: Real);

Triangle: (Side1, Side2, Angle: Real);
Circle: (Radius: Real);
Ellipse, Other: ();
end;
 
多人接受答案了。
 
后退
顶部