我使用了一个C++构建器函数,它允许我格式化从我的微控制器接收到的时间,如下所示:
void DisplayTime(unsigned long SecondsSince1900, unsigned short FractionOfSecond, AnsiString* DecodedString)
{
TDateTime WindowsDate;
double FloatingDate;
FloatingDate = SecondsSince1900 + (FractionOfSecond / 65536.0);
if ( (SecondsSince1900 & 0x80000000) == 0 )
{// Seconds since wraps around during year 2036.
// When the top bit is clear we assume that the date is after 2036 rather than before 1968.
FloatingDate += 0x100000000;//this line is the cause of the warning
}
FloatingDate /= SECONDS_IN_DAY ;
WindowsDate = FloatingDate ;
*DecodedString = FormatDateTime(" yyyy/mm/dd hh:mm:ss ", WindowsDate);
}使用此代码,我得到以下警告:
整数算术溢出
有什么办法可以避免这个问题吗?
发布于 2020-02-14 10:17:47
尽管一些编译器会将常量0x100000000解释为64位整数,但您的编译器似乎并非如此--这使得它太大,无法容纳32位整数(因此出现了警告)。
一个简单的方法是用一个double值替换整数常量:
FloatingDate += 4294967296.0;或者(如果编译器支持它),您可以将uLL后缀添加到整数常量中:
FloatingDate += 0x100000000uLL;但这可能会导致不同的警告(从unsigned long long转换到double可能导致精度下降)。
https://stackoverflow.com/questions/60224140
复制相似问题