1

I am attempting to store a variable length number that can have leading zeros as a part of that number.

Is there a class in the .NET framework capable of storing values like this without losing information about leading zeros, and could surpass the upper limit of a long?

I am currently storing them in a class like this, is there any way I could write this class better in the event there isn't some struct or class available in the BCL:

[Serializable]
public class Number
{
    public int[] Array { get; set; }
    public int Length { get { return Array.Length; } }

    public Number(string number)
    {
        Array = new int[number.Length];

        for (int i = 0; i < number.Length; i++)
        {
            Array[i] = Convert.ToInt32(number[i].ToString());
        }
    }

    public Number(int[] array)
    {
        Array = array;
    }

    public int ToInt()
    {
        return Convert.ToInt32(ToString());
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder(Array.Length);

        foreach (int i in Array)
            sb.Append(i);

        return sb.ToString();
    }
}

The ability to use this as a struct would be very useful, as would the ability to check equality easily.

Items in bold/italic are the requirements of such a class.

Brett Allen
  • 5,297
  • 5
  • 32
  • 62
  • 1
    Aren't you wanting a string? I see no number operations... – Samuel Carrijo Jan 29 '10 at 22:07
  • 1
    If you really want something that can exceed the upper limit of the `long` type, you're going to need to rethink your `ToInt` method. – Dan Tao Jan 29 '10 at 22:12
  • Essentially yes, however I wanted to enforce at the class/struct level the inability to use non-numeric data, and to make it clear wherever it is used in code that this was the intention. – Brett Allen Jan 29 '10 at 22:14
  • Good point Dan, however that was added to support some values that were stored as strings, and converted to int. Messy way of doing things definitely. +1 for pointing that out, need to add a comment warning about that in the code. – Brett Allen Jan 29 '10 at 22:15
  • Why do you need the leading digits? What kind of "number" is this? I say "number" in quotes, because it sounds to me like what you're really storing is a code, like a SSN or zip-code or whatever, not actually a number. That it happens to only be allowed to have digits doesn't make it any less of a code. The normal way to store codes is through strings. – Lasse V. Karlsen Jan 29 '10 at 22:23
  • It's not what you're looking for, but there is an internal class in the `System` namespace called `Number`. It throws `FormatException`'s on certain bad WCF service calls. I mention it in case someone else searches `.NET Number class` and ends up here. http://reflector.webtropy.com/default.aspx/4@0/4@0/untmp/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/clr/src/BCL/System/Number@cs/1305376/Number@cs – Tim Goodman Oct 05 '12 at 18:40

4 Answers4

2

I'd suggest taking a look at this question about big integers in C#. You could expand them for the leading zeros issue.

Community
  • 1
  • 1
Samuel Carrijo
  • 17,449
  • 12
  • 49
  • 59
  • Thank you! Can definitely use that since need to rely on 3.5 for time being, and can add the code to handle leading zeros myself. – Brett Allen Jan 29 '10 at 22:18
2

I see a number of problems with your class that should be addressed with the redesign.

  • Your class represents a value and is mutable.
  • The constructor taking an int array parameter only copies the reference.
  • The value can be larger than long but is returned as an int from the ToInt() method.

The first thing I would do to shrink the size of this class is use the array of integers as a constant stream of bits which make up a binary representation of your number. This would take a lot more care in order to ensure correctness of any desired operations, but would save you a significant amount of space. If you do that, I would also store a variable to keep track of leading 0's, or perhaps a variable "total digits" semantics.

Sorry this isn't an answer to your question, but hopefully it will help you in redesigning this your class.

Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
0

The BigInteger in .Net 4.0 addresses your upper limit requirement.

I think you'd still need a class like this to handle your leading zeros requirement.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • I did notice that when searching to see if Int128 had been implemented in .NET yet when someone posted (then deleted) their comment about using long long. I suppose I could create a struct in .NET 4.0 to hold the BigNumber and a "length" value to determine what leading zeros are needed when padding. – Brett Allen Jan 29 '10 at 22:10
0

Until BigInteger is openly available, I would create your own class/struct with one public string property to get/set the initial number (this will maintain your leading zeros). Then have a ReadOnly property that parses the string and returns an IntX instance.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227