Convert size_t to integer

*爱你&永不变心* 提交于 2021-01-29 09:18:57

问题


I'm working with moderately sized integers which are of a size so that their logarithm would be an int. I'm using the GNU Multiprecision library (GMP) with the C programming language.

I wonder whether there exists a function that would convert the data type size_t into an int.


回答1:


Source: src size_t is implementation dependent yeah, and it's size may vary. You can try to typecast it to int but that may not be able to store as you already mentioned indirectly. But still you can work with strings

#include <iostream>
#include <string>
using namespace std;
int main()
{
    size_t x = 123456789012345678LL;
    std::cout<<(x)<<" "<<int(x)<<" "<<to_string(x);

And then use:

    mpz_init(n);
    mpz_set_ui(n,0);
    flag = mpz_set_str(n,x, 10);
    assert (flag == 0);
    printf ("n = ");
    mpz_out_str(stdout,10,n);
    printf ("\n");
}

(Never read about GMP before this question, but the approach should be correct. Hope it helps :-)



来源:https://stackoverflow.com/questions/62504040/convert-size-t-to-integer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!