天天看点

vc6++没有UINT16办法,以及测试short, unsigned short,__int16,__int32

在一个项目中,要求用VC6写DLL,其中有字段要求用UINT 16,vc6中没有UINT16。

UINT16,无符号int要16位,占2字节(1字节byte=8位bit),1111 1111 1111 1111,表示范围0~65535。

vc6++没有UINT16办法,以及测试short, unsigned short,__int16,__int32

在此,我想到用别的类型代替它,并对范围测试,

下面用vc6随便建一个程序,添加一个button双击添加代码:

0000 0000 0000 0000 ~ 1111 1111 1111 1111

short:默认带符号的,最高位1为符号位,表示范围 : -32768~32767

unsigned short:为不带符号的,表示范围:  0~65535

signed short :为带符号的,表示范围: -32768~32767

__int16,

__int32,这俩不知道是啥,不研究了,只是在vc6中无意看到有这种类型,等以后有空再研究。

<span style="white-space:pre">	</span>short a=1;
	unsigned short b=1;
	signed short c=1;
	__int16 d;
	__int32 e;

	CString strSho;
	int i;
	while(1)
	{
	if (a>0)
	{
		a++;
	}
	else
	{
		a= a-1;
		//strSho.Format("a = %d",a);
		//AfxMessageBox(strSho);
		break;
	}
	}

	while(1)
	{
		if (b>0)
		{
			b++;
		}
		else
		{
			b = b - 1;
			break;
		}
	}

	while(1)
	{
		if (c>0)
		{
			c++;
		}
		else
		{
			c = c - 1;
			break;
		}
	}

	while(1)
	{
		if (d>0)
		{
			d++;
		}
		else
		{
			d = d - 1;
			break;
		}
	}

	while(1)
	{
		if (e > 0)
		{
			e++;
		}
		else
		{
			e = e - 1;
			break;
		}
	}

	strSho.Format("MAX a = %d,MAX b = %d, MAX c = %d, MAX d = %d, MAX e = %d",a,b,c,d,e);
	AfxMessageBox(strSho);
           

结果:

a=32767;

b=65535;

c=32767;

d=-13109;//这个不认识,算了,扔了吧

e=-858983461;//这个也不认识

补充:

刚刚看了下,__int16,__int32的介绍,下面是msdn的说法:

https://msdn.microsoft.com/en-us/library/29dh1w7z.aspx

Microsoft C/C++ features support for sized integer types. You can declare 8-, 16-, 32-, or 64-bit integer variables by using the __intn type specifier, where n is 8, 16, 32, or 64. 

The following example declares one variable for each of these types of sized integers:

Copy 

__int8 nSmall;      // Declares 8-bit integer

__int16 nMedium;    // Declares 16-bit integer

__int32 nLarge;     // Declares 32-bit integer

__int64 nHuge;      // Declares 64-bit integer

The types __int8, __int16, and __int32 are synonyms for the ANSI types that have the same size, and are useful for writing portable code that behaves identically across multiple platforms. The __int8 data type is synonymous with type char, __int16 is synonymous with type short, and __int32 is synonymous with type int. The __int64 type has no ANSI equivalent.

Example

The following sample shows that an __intxx parameter will be promoted to int:

Copy 

// sized_int_types.cpp

#include <stdio.h>

void func(int i) {

    printf_s("%s\n", __FUNCTION__);

}

int main()

{

    __int8 i8 = 100;

    func(i8);   // no void func(__int8 i8) function

                // __int8 will be promoted to int

}

func