我必须使用返回int32_t的API . 但实际值可能是较小的有符号/无符号类型 . 要了解API返回ENUM值的确切类型 . 它看起来像这样:
typedef enum Int{
Int8,
Int16,
Int32,
Uint8,
Uint16,
Uint32
}IntT;
typedef struct{
IntT (*getType)();
void (*getInt)(int32_t* out);
}IntegerT;
我想通过知道枚举值将int32_t的值转换为实际类型 . 有时我甚至想在我的代码中将unsigned int32分配给unsigned int64变量 . 知道unsigned int32作为int32_t返回,如果值足够大,那么它在此类型中表示为负值,如果我只是static_cast它到uint64_t那么符号位被扩展以填充uint64_t中的所有高位这产生了与预期完全不同的无符号值 .
因此,我编写了一个强制转换函数,应该将更大的int类型或更小的int类型转换为正确的值 . 但是,我觉得这可能是一个已知问题,并且可能存在已有的解决方案 . 以下是功能 . 如果您认为可以改进或者有更好的解决方案,请通知我(虽然我已经使这个功能比我真正需要的更通用) .
编辑:使这个便携式的Endianness .
编辑:删除了有关签名/未签名比较的编译器警告 .
#include <limits>
#include <boost/static_assert.hpp> //BOOST_STATIC_ASSERT
#include <stdexcept>
#include <cstring>
#include <boost/type_traits/make_unsigned.hpp>
namespace Detail
{
/** This a implementation helper class for bitwise_int_cast function */
template<bool IsToTypeSigned, bool IsFromTypeSigned>
class Converter
{
public:
template<typename ToIntType, typename FromIntType>
ToIntType convert(FromIntType from) {
BOOST_STATIC_ASSERT(sizeof(from) == 0); //This prevents this generic implementation being compiled
return from;
}
};
/** From signed to signed */
template<>
template<typename ToIntType, typename FromIntType>
ToIntType Converter<true, true>::convert(FromIntType from)
{
BOOST_STATIC_ASSERT(std::numeric_limits<ToIntType>::is_signed && std::numeric_limits<FromIntType>::is_signed);
if((from < std::numeric_limits<ToIntType>::min()) ||
(from > std::numeric_limits<ToIntType>::max())
) {
throw std::runtime_error("Integer overflow in casting from large signed rvalue into smaller signed lvalue");
}
return static_cast<ToIntType>(from);
}
/** From signed to unsigned */
template<>
template<typename ToIntType, typename FromIntType>
ToIntType Converter<false, true>::convert(FromIntType from)
{
BOOST_STATIC_ASSERT(!std::numeric_limits<ToIntType>::is_signed && std::numeric_limits<FromIntType>::is_signed);
typedef typename boost::make_unsigned<FromIntType>::type unsignedType;
unsignedType unsignedIn = from;
if(std::numeric_limits<FromIntType>::digits < std::numeric_limits<ToIntType>::digits) {
if(from < 0) {
return unsignedIn;
}
} else {
if(from > 0) {
if (unsignedIn > std::numeric_limits<ToIntType>::max()) {
throw std::runtime_error("Integer overflow in casting from large signed rvalue into smaller unsigned lvalue");
}
} else if (from < 0) {
throw std::runtime_error("Integer overflow in casting from large signed rvalue into smaller unsigned lvalue");
}
}
return unsignedIn;
}
/** From unsigned to signed */
template<>
template<typename ToIntType, typename FromIntType>
ToIntType Converter<true, false>::convert(FromIntType from)
{
BOOST_STATIC_ASSERT(std::numeric_limits<ToIntType>::is_signed && !std::numeric_limits<FromIntType>::is_signed);
if(std::numeric_limits<ToIntType>::digits < std::numeric_limits<FromIntType>::digits) {
typename boost::make_unsigned<ToIntType>::type allBitsSet = -1; //e.g. 0xFFFF
if( from > allBitsSet) {
throw std::runtime_error("Integer overflow in casting from large unsigned rvalue into smaller signed lvalue");
}
}
return static_cast<ToIntType>(from);
}
/** From unsigned to unsigned */
template<>
template<typename ToIntType, typename FromIntType>
ToIntType Converter<false, false>::convert(FromIntType from)
{
if(from > std::numeric_limits<ToIntType>::max()) {
throw std::runtime_error("Integer overflow in casting from large unsigned rvalue into smaller unsigned lvalue");
}
return static_cast<ToIntType>(from);
}
}
/**
* This cast only cares about integer sizes not sign mismatch
* works only on two's complement (Big or Little Endian) Machines
*/
template<typename ToIntType, typename FromIntType>
inline ToIntType bitwise_int_cast(FromIntType from)
{
BOOST_STATIC_ASSERT(std::numeric_limits<ToIntType>::is_integer && std::numeric_limits<FromIntType>::is_integer);
Detail::Converter<std::numeric_limits<ToIntType>::is_signed, std::numeric_limits<FromIntType>::is_signed> converter;
return converter.template convert<ToIntType>(from);
}
1 回答
你可以使用一个联盟:
将值分配给int32:
然后从联合的适当成员访问该值 .
//编辑:
我测试了这个方法,因为我不确定字节顺序是否会在这里正确保存 . 在我的Linux上,下面的程序运行正常:
给出输出:
mergeval int8=[11]
// EDIT2:
是的,此方法可能无法在Big Endian计算机上运行 . 我没有,所以我无法测试 . 很抱歉以前没有提过它 . 我认为,如果我在上面的疑虑中提到了字节顺序,那么它或许不太清楚它是一个可能有限的解决方案 .