在 Windows 中将 __int64 转换为 long
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2221487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting __int64 to long in Windows
提问by Jay
How to convert __int64 to long in Windows (MSVC8 & MSVC6)?
如何在 Windows (MSVC8 & MSVC6) 中将 __int64 转换为 long?
Will a normal typecasting work?
正常的类型转换会起作用吗?
Also, how about converting long to __int64? If the long is a negative value, will it work?
另外,如何将 long 转换为 __int64?如果 long 是负值,它会起作用吗?
Note - I am talking of a scenario in which the __int64 variable will always contain a value which will not be more than 32 bits long.
注意 - 我说的是 __int64 变量将始终包含一个不超过 32 位长的值的场景。
回答by Jichao
1. Convert long to __int64
1. 将 long 转换为 __int64
Acorrding to MSDNon the __int64
keyword:
根据MSDN上的__int64
关键字:
The _ _int64 keyword declares a new type, a 64-bit (8-byte) integer. As with the int, short, and long types, the _ _int64 type has a corresponding unsigned version, so the _ _int64 keyword actually can be used to create two types.
The following code sample shows how to declare two 64-bit integers, one signed and the other unsigned:
__int64 signed_big_int; unsigned __int64 unsigned_big_int;
_ _int64 关键字声明了一个新类型,一个 64 位(8 字节)整数。与 int、short 和 long 类型一样,_ _int64 类型也有相应的无符号版本,因此 _ _int64 关键字实际上可以用于创建两种类型。
以下代码示例显示了如何声明两个 64 位整数,一个有符号,另一个无符号:
__int64 签名_big_int; 无符号 __int64 unsigned_big_int;
__int64
is signed,and it should be wider than long
.So you could assign long
to __int64
without even a type cast and of course the signed __int64
support negative long.
__int64
是有符号的,它应该比long
. 所以你可以分配long
给__int64
甚至没有类型转换,当然signed __int64
支持负长。
2. Convert __int64 to long
2. 将 __int64 转换为 long
It is OK to convert __int64
to long
,only with possibility of loosing data.My msvc8 only warn me of the pssibility of loss of data.
可以转换__int64
为long
,只有丢失数据的可能性。我的 msvc8 只警告我丢失数据的可能性。
3. Note:
3. 注意:
C99 defined a standard 64-bit integer type named int64_t
and unsigned version uint64_t
in stdint.h
.If you want to provide portable code, you should use them but not __int64
.
C99定义的标准64位整数命名类型int64_t
和无符号版本uint64_t
中stdint.h
。如果你希望提供可移植的代码,你应该使用他们,但没有__int64
。
Notice there is no standard 64-bit integer type in C++ programming language,MSVC use __int64
,but in linux world you normally use int64_t
or uint64_t
which is type defined as long long
or unsigned long long
in C99's stdint.h
.Here I assume your C++ compiler support the stdint.h
header file.
公告中有C ++编程语言,MSVC没有使用标准的64位整数类型__int64
,但在Linux世界中通常使用int64_t
或者uint64_t
是定义为类型long long
或unsigned long long
在C99的stdint.h
。在这里我假设你的C ++编译器支持的stdint.h
头文件。
回答by Paul R
Yes, typecasting will be fine, so long as you can guarantee your __int64 values are always within the range of a long. Casting in the other direction, i.e. from long to __int64 will not be a problem regardless of the values involved.
是的,类型转换没问题,只要你能保证你的 __int64 值总是在 long 的范围内。向另一个方向转换,即从 long 到 __int64 不会有问题,无论涉及到什么值。
回答by Mark Wilkins
Here is a small test. The explicit casts are necessary to avoid the warnings if compiled with /W3
:
这是一个小测试。如果使用/W3
以下命令编译,则需要显式转换以避免警告:
#include <limits.h>
int main( int argc, char *argv[] )
{
__int64 i64;
long i;
i64 = -1;
i = (long)i64;
printf( "i=%d\n", i );
i64 = INT_MAX;
i = (long)i64;
printf( "i=%d\n", i );
i64 = INT_MIN;
i = (long)i64;
printf( "i=%d\n", i );
i64 = i;
printf( "i64=%I64d\n", i64 );
}
The output is:
输出是:
i=-1
i=2147483647
i=-2147483648
i64=-2147483648
回答by FrankieTheSkin
32-bit values can be assigned to 64-bit variables without any problems, signed or unsigned. Assigning a 64-bit value to a 32-bit variable can be done, but loss of data is possible if more than 32 bits are required to store the value, as it will be truncated to fit into the smaller variable.
可以将 32 位值分配给 64 位变量,没有任何问题,有符号或无符号。可以将 64 位值分配给 32 位变量,但如果需要超过 32 位来存储该值,则可能会丢失数据,因为它将被截断以适应较小的变量。