Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I want convert BIG integer value to string
for that i used
_strtoi64
that convert values still 9,223,372,036,854,775,807

but for big value greated than "9,223,372,036,854,775,807" it gives wrong value

have i convert greated than "9,223,372,036,854,775,807" value to string??.
Posted
Updated 12-Jan-12 1:25am
v2

What did you expect? A 64 bit integer can handle values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Outside that scope, you'll get an overflow.

If you need bigger numbers than that, you should google for libraries that can handle numbers of unlimited size. That is, if you really need the precision: you could also convert the value to double; in that case you'd still get a meaningful number, but the least significant digits will be cut off. If that is acceptable for your application, use that.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 12-Jan-12 7:55am    
Good answer! 5+
I'd really like to know what kind of application OP is after here. Running into the limits of a 64bit integer seems an indicator of some sort of design flaw, but as long as we don't know what OP is trying to achieve there is no point in reasoning about it.

Cheers!
Stefan_Lang 12-Jan-12 8:01am    
Thanks.

There's one application that I know of that requires very big numbers, and that is finding big primes, e.g. for RSA encoding. There's also quite a lot of theoretical work that math people may be involved in, which deals with big primes or 'pseudo primes'.

Other than that, there may be applications that use big numbers as identifiers of some sort, but in that case it may be easier to not try convert the numbers and just store them as strings instead, since they're not used to do real math.
This is not a simple question to answer: a 64 bit integer (as used as output by _strtoi64) can only hold 64 bit values, and is a signed quantity so you lose one bit to the sign. So when you try to convert string values that are bigger than this, they just don't fit, so it gives you the largest value it can hold: 9,223,372,036,854,775,807

The problem is that not knowing anything about your application, I can't suggest an alternative, because in order to use numbers larger than this you have to abandon 64 bit values throughout - or move to floating point representations.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Jan-12 7:53am    
That just about sums it up nicely! 5+
If you don´t want to use other library but increase your num support you could try:

_strtoui64() which uses unsigned int and gives you more range:

0 to 18,446,744,073,709,551,615 (_UI64_MAX)

Of course, the variable should be unsigned __int64.
 
Share this answer
 
You may use a Big Integer library for the purpose. See instance "C++ Big Integer Library"[^]: if you check out the sample.cc file, therein you may find an example of such a conversion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900