Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to count the total amount of digits in a byte[] if you were to sum the array

i want to make a function that counts the amount of digits once the value is sumed up

lets say i have this array

byte[] array = new byte[] { 200, 100, 200, 250, 150, 100, 200 };

once this is sumed up you’ll have a value of 1200

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you can get the amount of digits with these functions

Math.Floor(Math.Log10(1200)+1) // 4

but if i sum it up and there are too many values in the array i get an integer overflow

public decimal countDigits(byte[] array)
{
    decimal count = array[0];

    for (int i = 1; i < array.Length; i++)
    {
         count = Math.Log10(Math.Pow(count, 10)+array[i]);
    }

    return count;
}

this does give the correct output i want but this causes a integeroverflow if the count is greater than 28.898879583742193 (log10(decimal.MaxValue))

>Solution :

There’s a BigInteger data type that can sum up arbitrary (integer) values. It even has a Log10 method, so it works very similar to a standard integer variable. The only limitation is that the result of BigInteger.Log10 must be smaller than Double.MaxValue, but that sounds like a reasonable limitation. (10^1E308=10^10^308 is a really huge number)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading