【问题标题】:type cast CryptoPP::Integer to int将 CryptoPP::Integer 类型转换为 int
【发布时间】:2017-06-29 19:05:20
【问题描述】:
我没有任何 Crypto++ 库的经验。在我的项目中,我需要将Integer 转换为int。这就是我正在尝试的:
int low_bound1=8;
int low_bound2=9;
Integer x=1,y=2;
low_bound1=(int)x;
low_bound1=(int)y;
这是我得到的错误:
error: invalid cast from type 'CryptoPP::Integer' to type 'int'
有可能吗?如果是那怎么办?
【问题讨论】:
标签:
c++
type-conversion
crypto++
【解决方案1】:
有可能吗?如果是那怎么办?
是的,它可能可以做到,但不能使用简单的 C 风格转换。
这是手册中 Integer 类的文档:Integer Class Reference。在 ACCESSORS 标题下,有两种方法:
bool IsConvertableToLong () const
确定 Integer 是否可转换为 Long。更多...
signed long ConvertToLong () const
将整数转换为长整数。更多...
所以你需要做类似的事情:
int low_bound1, low_bound2;
Integer x=1,y=2;
if (x > std::numeric_limits<int>::max() || x < std::numeric_limits<int>::min())
throw std::out_of_range("Integer x does not fit int data type");
if (y > std::numeric_limits<int>::max() || y < std::numeric_limits<int>::min())
throw std::out_of_range("Integer y does not fit int data type");
low_bound1 = static_cast<int>(x.ConvertToLong());
low_bound2 = static_cast<int>(y.ConvertToLong());