简体   繁体   English

如何将32位有符号整数值转换为C中等效的64位有符号整数

[英]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.

相关问题 如何将32位有符号整数放入64位无符号整数的高32位? - How to put 32-bit signed integer into higher 32 bits of 64-bit unsigned integer? C ++读取带符号的32位整数值 - C++ Reading signed 32-bit integer value 将32位大字节序有符号整数转换为有符号小字节序整数 - convert 32 bit big endian signed integer to signed little endian integer 如何在 C++ 中使用 integer 限制。数字“-91283472332”超出了 32 位有符号 integer 的范围 - How to work with integer limits in C++. The number "-91283472332" is out of the range of a 32-bit signed integer C ++:带符号64位整数中两个无符号64位整数的差值 - C++: Difference of two unsigned 64-bit integer in a signed 64-bit integer C/C++ - 将 24 位有符号 integer 转换为浮点数 - C/C++ - Convert 24-bit signed integer to float 如何在Java中将带符号的16位整数转换为无符号16位整数? - How to convert signed 16 bit integer to unsigned 16 bit integer in Java? 将64位整数中的所有其他位与32位整数进行比较 - Comparing every other bit in a 64 bit integer to a 32 bit integer 在 C 中进行移位的复合运算符或如何将 20 位 2' 补数转换为 32 位有符号整数 - Compound operator with shift in C or how to convert 20bit 2'complement number in 32bit signed int 如何将 64 位 integer 存储在两个 32 位整数中并再次转换回来 - How to store a 64 bit integer in two 32 bit integers and convert back again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM