78553

BigInteger.valueOf() for very big numbers?

Question:

What would be the best way to convert a 50-digit String to a BigInteger in Java? It doesn't have a valueOf(String) method, and I can't convert to Long because it's too small.

Answer1:

It does have a <a href="http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String)" rel="nofollow">BigInteger(String)</a> constructor :-)

String S = "12345678901234567890123456789012345678901234567890"; BigInteger bi = new BigInteger(S);

Answer2:

How about...

BigInteger bi = new BigInteger(my50DigitString);

All those Xxx.valueOf() methods are alternatives to constructors because they allow for returning shared, cached copies. Constructors, by definition, return a new instance every time. So valueOf() are a nice optimization, but the designers apparently didn't find it interesting to provide a BigInteger.valueOf(String) method. You'll have to use a one of the constructors in this case.

Answer3:

BigInteger has a constructor that takes a String.

Answer4:

Have you tried

BigInteger i = new BigInteger(s);

Recommend

  • Wordpress Mobile - Change Part of the URL Instead of Redirecting to the Front Page?
  • High quality image re-sampling in Mono/C#/ASP.NET
  • Odd Error Importing DOT files
  • java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported major.minor version
  • Flushing the HTML document early - with ini_set( 'zlib.output_compression','ON')
  • How to get intermediate colors from one to another? [duplicate]
  • Filter Values of Current Week with XQuery
  • Returning 100-Continue from PHP/Apache
  • How to use the WinInet API from Java?
  • Alternative to ReportViewer [closed]
  • JQuery animation: Is it possible to change speed during the animation?
  • Android: Alternative to onSensorChanged? no ACCELEROMETER data when phone is lying on its back on a
  • Segmentation fault when accessing a list inside a struct
  • Is TWebBrowser dependant on IE version?
  • declaring device constant in terms of another constant
  • Indexing k-d tree?
  • Why is new Number(8) not exactly equal to 8?
  • Help translating Reflector deconstruction into compilable code
  • iOS - Is this a task for enums?
  • Parse Framework with Swift
  • Validation fired but Red Border does not appear with User Control in Silverlight 4
  • custom string delimiters stringtemplate-4
  • How to convert integer to string and get length of string
  • Bad interaction between Zope2 XML-RPC and AT Image mutator?
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Blackberry 6: how to detect a long click on track pad?
  • unable to get jsonEncode in magento2
  • How to pass nginx proxy url for socket
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • How can I set a binding to a Combox in a UserControl?
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Create DicomImage from scratch using Dcmtk
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to do unit test for HttpContext.Current.Server.MapPath
  • Azure Cloud Service Web Role web pages do not load
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Cannot Parse HTML Data Using Android / JSOUP
  • C# - Getting references of reference
  • Angular 2 constructor injection vs direct access