Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6 VC7 VC7.1 C++ C MFC
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 12-Jan-12 2:16am
Edited 12-Jan-12 2:25am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
Manfred R. Bihy at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Manfred R. Bihy at 12-Jan-12 7:53am
   
That just about sums it up nicely! 5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 305
2 Richard MacCutchan 270
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,834
1 Sergey Alexandrovich Kryukov 7,477
2 DamithSL 5,689
3 Maciej Los 5,329
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 12 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100