[英]How to convert a 32 bit signed integer value to 64 bit signed integer equivalent in C
I have a library that is compiled to use 32 bit signed integer. 我有一个编译为使用32位有符号整数的库。 When other applications compile theirs with a flag eg: -DODBC64 it promotes the same type I have used in my library to a 64 bit signed integer.
当其他应用程序使用标志编译它们时,例如:-DODBC64它将我在库中使用的相同类型提升为64位有符号整数。 eg:
例如:
#ifdef ODBC64
typedef sint64 SLEN;
#else
#define SLEN int
#endif
When the application passes reference to my library as : 当应用程序将引用传递给我的库时:
SLEN count;
mylibraryfunction(&count);
the values returned to application looks like these: 返回给应用程序的值如下所示:
sizeof(SLEN) = 8
sizeof(SLEN) in my library = 4
m_AffectedRows BEFORE = 0x3030303030303030
m_AffectedRows AFTER = 0x3030303000000000 0
You can see that the assignment from my lib is copying 4 bytes (value 0). 您可以看到我的lib中的赋值是复制4个字节(值0)。 I need to know a way to reset the upper 4 bytes to 0. eg:
我需要知道一种方法将高4字节重置为0.例如:
0x0000000000000000
I have tried both static_cast and reinterpret_cast, but none are helpful. 我已经尝试过static_cast和reinterpret_cast,但没有一个是有帮助的。
I made a MCVE where I resembled what happens in OPs case. 我做了一个MCVE,我在OPs案例中发生了类似的事情。
I even didn't need an extra library for this, just two translation units (resulting in two object files). 我甚至不需要额外的库,只需要两个翻译单元(产生两个目标文件)。
First lib.cc
: 首先是
lib.cc
:
#include <cstdint>
extern "C" void func(int32_t *pValue);
void func(std::int32_t *pValue)
{
*pValue = 0;
}
Second prog.cc
: 第二个
prog.cc
:
#include <iostream>
#include <iomanip>
// how prog.cc "knows" func():
extern "C" void func(int64_t *pValue);
int main()
{
int64_t value = 0x0123456789ABCDEFull;
std::cout << "value before: " << std::hex << value << '\n';
func(&value);
std::cout << "value after : " << std::hex << value << '\n';
return 0;
}
Compiler errors? 编译错误? No. Each translation unit uses prototype of
func()
conformant. 不。每个翻译单元都使用符合
func()
原型。
Linker errors? 链接器错误? No. The symbols match, anything else is beyond view of linker.
不。符号匹配,其他任何东西都超出链接器的视野。
I must admit I had to use extern "C"
to achieve this. 我必须承认我必须使用
extern "C"
来实现这一目标。 Otherwise, at least, the C++ name mangling had prevented the proper linking. 否则,至少,C ++名称修改阻止了正确的链接。 (When I became aware of this, I made code in C.)
(当我意识到这一点时,我在C中制作了代码。)
Output: 输出:
value before: 123456789abcdef
value after : 123456700000000
Live Demo on wandbox 在wandbox上现场演示
This is very dangerous! 这非常危险! Any use of any extern symbol should use a 100 % compatible declaration.
任何使用任何外部符号都应使用100%兼容的声明。 (And yes, C and C++ provide various ways to shoot into your own foot.)
(是的,C和C ++提供了各种方法来拍摄自己的脚。)
Imagine what would happen if the lib.cc function func()
would write int64_t
where the prog.cc would pass a pointer to int32_t
: Out of bound access with possible more disastrous consequences. 想象一下,如果lib.cc函数
func()
将int64_t
写入prog.cc将指针传递给int32_t
情况下会发生什么:超出绑定的访问可能带来更多灾难性的后果。
Your library can't access the upper 4 bytes, but you can before you call it. 您的库无法访问大写的4个字节,但您可以在调用它之前访问它。 So try to initialize it with 0's first:
所以尝试先用0初始化它:
SLEN count = 0; // initialize with 0's
mylibraryfunction(&count);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.