您的位置:首页 > 编程语言 > C语言/C++

C++与C#数据类型对照

2013-05-20 09:49 295 查看
C++ C#

=====================================

WORD ushort

DWORD uint

UCHAR int/byte 大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte

UCHAR* string/IntPtr

unsigned char* [MarshalAs(UnmanagedType.LPArray)]byte[]/?(Intptr)

char* string

LPCTSTR string

LPTSTR [MarshalAs(UnmanagedType.LPTStr)] string

long int

ulong uint

Handle IntPtr

HWND IntPtr

void* IntPtr

int int

int* ref int

*int IntPtr

unsigned int uint

COLORREF uint

API与C#的数据类型对应关系表

API数据类型 类型描述 C#类型 API数据类型 类型描述 C#类型

WORD 16位无符号整数 ushort CHAR 字符 char

LONG 32位无符号整数 int DWORDLONG 64位长整数 long

DWORD 32位无符号整数 uint HDC 设备描述表句柄 int

HANDLE 句柄,32位整数 int HGDIOBJ GDI对象句柄 int

UINT 32位无符号整数 uint HINSTANCE 实例句柄 int

BOOL 32位布尔型整数 bool HWM 窗口句柄 int

LPSTR 指向字符的32位指针 string HPARAM 32位消息参数 int

LPCSTR 指向常字符的32位指针 String LPARAM 32位消息参数 int

BYTE 字节 byte WPARAM 32位消息参数 int

BOOL=System.Int32

BOOLEAN=System.Int32

BYTE=System.UInt16

CHAR=System.Int16

COLORREF=System.UInt32

DWORD=System.UInt32

DWORD32=System.UInt32

DWORD64=System.UInt64

FLOAT=System.Float

HACCEL=System.IntPtr

HANDLE=System.IntPtr

HBITMAP=System.IntPtr

HBRUSH=System.IntPtr

HCONV=System.IntPtr

HCONVLIST=System.IntPtr

HCURSOR=System.IntPtr

HDC=System.IntPtr

HDDEDATA=System.IntPtr

HDESK=System.IntPtr

HDROP=System.IntPtr

HDWP=System.IntPtr

HENHMETAFILE=System.IntPtr

HFILE=System.IntPtr

HFONT=System.IntPtr

HGDIOBJ=System.IntPtr

HGLOBAL=System.IntPtr

HHOOK=System.IntPtr

HICON=System.IntPtr

HIMAGELIST=System.IntPtr

HIMC=System.IntPtr

HINSTANCE=System.IntPtr

HKEY=System.IntPtr

HLOCAL=System.IntPtr

HMENU=System.IntPtr

HMETAFILE=System.IntPtr

HMODULE=System.IntPtr

HMONITOR=System.IntPtr

HPALETTE=System.IntPtr

HPEN=System.IntPtr

HRGN=System.IntPtr

HRSRC=System.IntPtr

HSZ=System.IntPtr

HWINSTA=System.IntPtr

HWND=System.IntPtr

INT=System.Int32

INT32=System.Int32

INT64=System.Int64

LONG=System.Int32

LONG32=System.Int32

LONG64=System.Int64

LONGLONG=System.Int64

LPARAM=System.IntPtr

LPBOOL=System.Int16[]

LPBYTE=System.UInt16[]

LPCOLORREF=System.UInt32[]

LPCSTR=System.String

LPCTSTR=System.String

LPCVOID=System.UInt32

LPCWSTR=System.String

LPDWORD=System.UInt32[]

LPHANDLE=System.UInt32

LPINT=System.Int32[]

LPLONG=System.Int32[]

LPSTR=System.String

LPTSTR=System.String

LPVOID=System.UInt32

LPWORD=System.Int32[]

LPWSTR=System.String

LRESULT=System.IntPtr

PBOOL=System.Int16[]

PBOOLEAN=System.Int16[]

PBYTE=System.UInt16[]

PCHAR=System.Char[]

PCSTR=System.String

PCTSTR=System.String

PCWCH=System.UInt32

PCWSTR=System.UInt32

PDWORD=System.Int32[]

PFLOAT=System.Float[]

PHANDLE=System.UInt32

PHKEY=System.UInt32

PINT=System.Int32[]

PLCID=System.UInt32

PLONG=System.Int32[]

PLUID=System.UInt32

PSHORT=System.Int16[]

PSTR=System.String

PTBYTE=System.Char[]

PTCHAR=System.Char[]

PTSTR=System.String

PUCHAR=System.Char[]

PUINT=System.UInt32[]

PULONG=System.UInt32[]

PUSHORT=System.UInt16[]

PVOID=System.UInt32

PWCHAR=System.Char[]

PWORD=System.Int16[]

PWSTR=System.String

REGSAM=System.UInt32

SC_HANDLE=System.IntPtr

SC_LOCK=System.IntPtr

SHORT=System.Int16

SIZE_T=System.UInt32

SSIZE_=System.UInt32

TBYTE=System.Char

TCHAR=System.Char

UCHAR=System.Byte

UINT=System.UInt32

UINT32=System.UInt32

UINT64=System.UInt64

ULONG=System.UInt32

ULONG32=System.UInt32

ULONG64=System.UInt64

ULONGLONG=System.UInt64

USHORT=System.UInt16

WORD=System.UInt16

WPARAM=System.IntPtr

<---------补充----------->

Wtypes.h 中的非托管类型 非托管C 语言类型 托管类名 说明

HANDLE void* System.IntPtr 32 位

BYTE unsigned char System.Byte 8 位

SHORT short System.Int16 16 位

WORD unsigned short System.UInt16 16 位

INT int System.Int32 32 位

UINT unsigned int System.UInt32 32 位

LONG long System.Int32 32 位

BOOL long System.Int32 32 位

DWORD unsigned long System.UInt32 32 位

ULONG unsigned long System.UInt32 32 位

CHAR char System.Char 用 ANSI 修饰。

LPSTR char* System.String 或 System.StringBuilder 用 ANSI 修饰。

LPCSTR Const char* System.String 或 System.StringBuilder 用 ANSI 修饰。

LPWSTR wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。

LPCWSTR Const wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。

FLOAT Float System.Single 32 位

DOUBLE Double System.Double 64 位

C++数据类型

新术语 C++数据类型定义编译器在内存中存放信息的方式。在有些编程语言中,可以向变量赋予任何数值类型。例如,下面是BASIC代码的例子:x = 1;x = 1000;x = 3.14;x = 457000;在BASIC中,翻译器能考虑根据数字长度和类型分配空间。而在C++,则必须先声明变量类型再使用变量:int x1 = 1;int x = 1000;float y = 3.14;long z = 457000;这样,编译器就可以进行类型检查,确保程序运行时一切顺利。数据类型使用不当会导致编译错误或警告,以便分析和纠正之后再运行。有些数据类型有带符号和无符号两种。带符号(signed)数据类型可以包含正数和负数,而无符号(unsigned)数据类型只能包含正数。表1.1列出了C++中的数据类型、所要内存量和可能的取值范围。

表1.1C++数据类型(32位程序)

数据类型 字节 数取值范围

char 1 -128到126

unsigned char 1 0到255

short 2 -32,768到32,767

unsigned short 2 0到65,535

long 4 -2,147,483,648到2,147,483,648

unsigned long 4 0到4,294,967,295

int 4 同long

unsigned int 4 同unsigned long

float 4 1.2E-38到3.4E381

double 8 2.2E-308到1.8E3082

bool 1 true或false

从上表可以看出,int与long相同。那么,为什么C++还要区分这两种数据类型呢?实际上这是个遗留问题。在16位编程环境中,int要求2个字节而 long要求4个字节。而在32位编程环境中,这两种数据都用4个字节存放。C++Builder只生成32位程序,所以int与long相同。说明 在 C++ Builder和BorLand C++ 5.0中,Bool是个真正的数据类型。有些C++编译器有Bool关键字,则Bool不是个真正的数据类型。有时Bool只是个typedef,使Bool等价于int。typedef实际上建立别名,使编译器在一个符号与另一符号间划上等号。
typedef的语法如下:typedef int Bool;这就告诉编译器:Bool是int的别名。说明 只有double和float数据类型使用浮点数(带小数点的数)。其它数据类型只涉及整数值。尽管integer数据类型也可以指定带小数点的数值,但小数部分会舍弃,只将整数部分赋予整型变量,例如:int x=3.75;得到的x取值为3。注意,这个整数值并不是四舍五入,而是放弃小数部分。顺便说一句,大多数Windows程序很少用到浮点数。C++可以在必要时进行不同数据类型间的换算。例如:short result;long
num1 = 200; long num2 = 200;result = num1 * num2;这里我想将两个长整型的积赋予一个短整型。尽管这个公式混用了两种数据类型,但C++能够进行换算。计算结果会怎样呢?结果会让你大吃一惊,是25536,这是绕接(wrop)的结果。从表1.1可以看出,短整型的最大取值为 32767,在最大值之上加1会怎么样呢?得到的是32768。这实际上与汽车里程计从99999回到00000的道理一样。为了说明这点,请输入并运行下列清单1.3中包含的程序。

清单1.3Wrapme.cpp

1: #include <iostream.h>

2: #include <conio.h>

3: #pragma hdrstop

4:

5: int main(int argc,char **argv)

6: {

7:short x = 32767;

8:cout << " x = " << x << endl;

9:x++;

10: cout << " x = " << x << endl;

11: getch();

12: return 0;

13: }

40000 - 32768 -32768 = -25536;

说明后面几节要介绍的有些清单没有下列语句:

#include<condefs.h>

C++ Builder 生成新的控制台应用程序时会自动加上这条语句。这在你所用的程序中不是必须的,所以代码清单中将其省略。无论有无这条语句,程序运行结果是一致的。分析输出结果为:x=32767 x=32768如果用int数据类型,则不会有这个问题,因为int数据类型的取值范围在正向20亿之间,一般不会有绕回的问题。但这时程序可能会稍大一些,因为int需要4字节存储,而short只需要2字节存储。对于大多数应用程序,这个差别是不显著的。前面介绍了自动类型换算。有时C++无法进行换算,这时可能在编译器中产生编译错误,说Cannot
convert from x to y(无法从x换算到Y)。编译器也可能警告说Conversion might lose significant digits(换算可能丢失显著位)。提示 编译器警告应当作编译器错误,因为它表明出了错误。我们应努力生成无警告的编译。有时警告无法避免,但一定要认真检查所有警告。应充分了解警告的原因并尽量予以纠正。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/okadler0518/archive/2009/06/22/4289679.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: