Adding two very large numbers represented as base64-encoded strings
I have very large numbers开发者_JAVA技巧, and decided to represent them with base64 strings in php. I was wondering if anyone knows of a library (or built in system) to work with base64 as numbers (aka add, subtract, multiply, divide, etc)?
Generally it does not make much sense to speak about operations on numbers in one specific base or another. Rather, you decode your numbers to a more useful or generalized representation (e.g. a string of decimal digits) and then perform work, re-encoding your result for storage as necessary.
The Math_BigInteger library facilitates this. You will of course still have to first decode your base64 data to a base usable by the class, i.e., binary, decimal, or hexidecimal.
This is an instance of the XY problem. The problem is that you want to add arbitrary-precision numbers. For that, you should use an arbitrary precision math package, such as BC. There's no point representing them in base64 unless there's an implementation of arithmetic for those, which as far as you know there isn't.
精彩评论