How to perform mathematical operations with generic variables?

I am working on a project that requires string representations of integers, but using odd number bases that are not natively supported by the .NET Framework (as far as I know) - for example, base36, 62, 64, etc.
We decided to write a general string conversion system that will work with any number base, since this is a fairly simple operation.

Later we want to create the IFormatProvider / ICustomFormatter user interface to make it easier to use in the future.
But first, we want to smooth out the conversion process itself in the form of some static methods that perform conversions and return some basic results.
Once we get this working, we will put the IFormatProvider wrappers in place.

Some time has passed since I worked with C # generators, and I don’t remember how to make the compiler happy with this operation.
I would prefer to create a private static general method ConvertInteger, which I can then call from public static methods Convertto help provide strong typing without making it useless.
One of the main reasons for this setting is to prevent sign bit problems when converting signed and unsigned values.
Right now we have static methods Convertfor longand ulongin order to avoid conversion problems between values ​​signed and unsigned.
In the future, assuming that we can make a private static universal method work, we would like to extend the public methods to include int, uint, short, ushort, byte and sbyte as explicit implementations to help with performance when executing this method for large batches of values ​​of various integer sizes.
This is where the universal method design comes in handy, so we don’t need to repeat the same code over and over (simplifies testing and debugging).

, , , , , , .
( , , ++, , ++ , , .)

, ?
, , .

public static class NumericStringConverter
{
    private static readonly string[] StandardDigits = new string[]
    {
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
        "_", "-"
    };



    private static string ConvertInteger<T>(T Value, T Base)
    {
        if (Base < 2) // error here: "Operator '<' cannot be applied to operands of type 'T' and 'int'"
            throw new ArgumentOutOfRangeException("Base", Base, "The NumericStringConverter.Convert(Value, Base) method was called, with the Base parameter set to a value less than 2.");

        if (Base > 64) // error here: "Operator '>' cannot be applied to operands of type 'T' and 'int'"
            throw new ArgumentOutOfRangeException("Base", Base, "The NumericStringConverter.Convert(Value, Base) method was called, with the Base parameter set to a value greater than 64.");

        if (Value == 0) // error here: "Operator '==' cannot be applied to operands of type 'T' and 'int'"
            return StandardDigits[0];

        string strResult = "";
        bool IsValueNegative = (Value < 0); // error here: "Operator '<' cannot be applied to operands of type 'T' and 'int'"

        while (Value != 0) // error here: "Operator '!=' cannot be applied to operands of type 'T' and 'int'"
        {
            strResult = strResult.Insert(0, StandardDigits[Math.Abs(Value % Base)]); // error here: "Operator '%' cannot be applied to operands of type 'T' and 'T'"
            Value /= Base; // error here: "Operator '/=' cannot be applied to operands of type 'T' and 'T'"
        }

        if (IsValueNegative)
            strResult = strResult.Insert(0, "-");

        return strResult;
    }



    public static string Convert(long Value, long Base)
    {
        return ConvertInteger<long>(Value, Base);
    }

    public static string Convert(ulong Value, ulong Base)
    {
        return ConvertInteger<ulong>(Value, Base);
    }

    public static T Convert<T>(string Value, T Base)
    {
        return default(T); // TODO: convert a string back into an integer value.
    }
}
+4
1

, , , longs int:

private static string ConvertInteger(long Value, byte Base)
{
    if (Base < 2)
        throw new ArgumentOutOfRangeException("Base", Base, "The NumericStringConverter.Convert(Value, Base) method was called, with the Base parameter set to a value less than 2.");

    if (Base > 64) 
        throw new ArgumentOutOfRangeException("Base", Base, "The NumericStringConverter.Convert(Value, Base) method was called, with the Base parameter set to a value greater than 64.");

    if (Value == 0) 
        return StandardDigits[0];

    string strResult = "";
    bool IsValueNegative = (Value < 0); 

    while (Value != 0) 
    {
        strResult = strResult.Insert(0, StandardDigits[Math.Abs(Value % Base)]); 
        Value /= Base; 
    }

    if (IsValueNegative)
        strResult = strResult.Insert(0, "-");

    return strResult;
}
public static string Convert(int Value, byte Base)
{
    return ConvertInteger((long)Value, Base);
}

public static string Convert(ulong Value, byte Base)
{
    return ConvertInteger((long)Value, Base); // check for overflow?
}
+4

All Articles