2017-02-11 90 views
1

我没有任何Crypto ++库的经验。在我的项目中,我需要输入Integerint。这就是我想:类型cast CryptoPP :: 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' 

是否有可能呢?如果是,那么如何?

+1

这是一个多精度整数(它可以存储值太大,不能用内建类型持有):具有转换方法:HTTPS ://www.cryptopp.com/docs/ref/class_integer.html例如https://www.cryptopp.com/docs/ref/class_integer.html#a2e90d8f4c5a13e203b94f9abc24d733f –

+0

谢谢,它帮助! –

回答

0

可以吗?如果是,那么如何?

是的,它可能可能做到,但不是一个简单的C风格演员。

以下是手册中Integer类的文档:Integer Class Reference。在标题存取,有两种方法:

bool IsConvertableToLong() const
确定是否该整数是可转换到长。更多...

signed long ConvertToLong() const
将Integer转换为Long。更多...

所以,你需要做的是这样的:

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()); 
+0

感谢您的帮助! –