An integer measures 4 bytes. In my example I have numbers measuring 1 MB. How can I convert them to a human readable decimal number fast?
The number is present in uint[] Array containing Size items.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t know if this is any faster, but here is an example in delphi I wrote a long time ago to handle big ints as strings (VERY quick and dirty) – this was for 128bit uint but you could extend it indefinitely
Then take the concatenated binary string and add powers of two each time you see a ‘1’
generateCard is used to create a decimal string representation of 2^i (for i>0)
and MulByTwo multiplies a decimal string by two
And finally – AddDecimalStrings…well, it adds two decimal strings :
These functions allow you to perform basic arithmetic on almost arbitrarily large integers as strings. You hit another wall when the number of digits is too big to index an array with, of course.
Here’s a divide by two, btw (useful for going the other way…). I don’t handle odd numbers here.
EDIT: I just tried this with a 9million bit long binary string and it is ludicrously slow! Not surprising, really. This is completely unoptimized code which has a lot of low hanging fruit to pick at for speeding things up. Still, I can’t help but feel that this is the kind (or scale) of problem that you would probably want to write, at least partially, in fully optimized assembly. The individual operations are small but they must be done many times – that spells begging for assembly. Multithreading could certainly be leveraged here too.