Delphi数据类型如何翻译成C++的数据类型
2016-02-20 · 百度知道合伙人官方认证企业
C++与Delphi数据类型的比较:
转换:
#include <cstdlib>
#include <iostream>
#include "default.h"
using namespace std;
#pragma pack(push,1) //在定义结构的时候一顶要用到pack(push,1)和下面的pack(pop)
//否则你构造的结构的长度会有问题
typedef struct ethhdr //以太网头部,长度14
{
unsigned char dst[6]; //目的的MAC地址
unsigned char src[6]; //源的MAC地址
unsigned short type; //帧类型
}ETHHDR,*PETHDHR;
typedef struct eth_arphdr //以太网arp字段长度28
{
unsigned short arp_hrd; //硬件类型
unsigned short arp_pro; //协议类型
unsigned char arp_hln; //硬件地址长度(6)
unsigned char arp_pln; //协议地址长度(4)
unsigned short arp_op; //回应还是请求
unsigned char arp_sha[6]; //发送者MAC地址
unsigned long arp_spa; //发送者IP
unsigned char arp_tha[6]; //接收者MAC地址
unsigned long arp_tpa; //接收者IP
}ETH_ARPHDR,*PETH_ARPHDR;
typedef struct arp //整个ARP包的结构
{
ETHHDR ethhdr;
ETH_ARPHDR eth_arp;
}ARP,*PARP;
#pragma pack(pop)
int main(int argc, char *argv[])
{
ETHHDR eth;
ETH_ARPHDR arph;
ARP arp1;
cout << "ETHHDR:" <<sizeof(eth)<<endl;
cout << "ETH_ARPHDR:" <<sizeof(arph)<<endl;
cout << "ARP:" <<sizeof(arp1)<<endl;
/*
加#pragma pack(push,1)
ETHHDR:14
ETH_ARPHDR:28
ARP:42
*/
/*
不加#pragma pack(push,1)
ETHHDR:14
ETH_ARPHDR:32
ARP:48
*/
system("PAUSE");
return EXIT_SUCCESS;
}
ShortInt 8位有符号整型 signed char
SmallInt 16位有符号整型 short
LongInt 32位有符号整型 int
Byte 8位无符号整型 unsigned char
Word 16位无符号整型 unsigned short
Integer 32位有符号整型 int
Cardinal 32位无符号整型 unsigned int
Boolean 真/假 bool
ByteBool 真/假 或 8位无符号整型 unsigned char
WordBool 真/假 或 16位无符号整型 unsigned short
LongBool 真/假 或 32位无符号整型 BOOL (WinAPI)
AnsiChar 8位无符号字符 char
WideChar 宽字编码字符 wchar_t
Char 8位无符号字符 char
AnsiString Delphi的字符串类 AnsiString 类
String[n] 老式的Delphi字符串, n = 1..255字节 SmallString 模板类
ShortString 老式的Delphi字符串, 255字节 SmallString<255>
String Delphi的AnsiString类 AnsiString
Single 32位浮点数 float
Double 64位浮点数 double
Extended 80位浮点数 long double
Real 32位浮点数 double
Pointer 32无类型指针 void *
PChar 32位字符型指针 unsigned char *
PAnsiChar 32位ANSI型字符指针 unsigned char *
Comp 64位浮点数 Comp 类
OleVariant OLE可变类型值 OleVariant 类