Home > OS >  Generic integer type conversion from and to string
Generic integer type conversion from and to string

Time:05-28

I want to convert any integral type to an hexadecimal string representation and back. I came up with something like the following, but it obviously has errors. Any suggestion on doing that in C#? Thanks in advance.

public class HexCnv<T>
{
    public static T ToIntType(string sInput)
    {
        return T.Parse(sInput.TrimStart('0', 'x'));
    }

    public static string ToStringType(T nInput)
    {
        return "0x"   nInput.ToString("X2");
    }
}

CodePudding user response:

There is not a generic constraint that will limit generic parameters to integral types, and you can't call static methods (like Parse) on a generic type. You could either use implicit conversion and use long (or maybe ulong) everywhere, or provide overloads for each integral type - though you only need it for the ToIntType method since the ToStringType would give you the same result for a byte as a byte casted to a long.

CodePudding user response:

How's this work? (Not tested)

public class HexCnv<T>
{
    public static readonly TypeCode typeCode = Type.GetTypeCode(typeof(T));
    
    public static T ToIntType(string sInput)
    {
        switch (typeCode)
        {
            case TypeCode.Int32:
               return Int32.Parse(sInput.TrimStart('0', 'x'));
            case TypeCode.Int64:
               return Int64.Parse(sInput.TrimStart('0', 'x'));
            ...
            ...
            ...
        }
        throw new ArgumentException(nameof(sInput));
    }

    public static string ToStringType(T nInput)
    {
        return "0x"   nInput.ToString("X2");
    }
}

CodePudding user response:

I found a solution with all your ideas, thanks a lot. I've tested the following code, it works the way I want and I made it with a generic sense; I could test other methods to check code efficiency, but it works for me and I hope this idea works for anyone else:

public class HexCnv<T> 
{
    private MethodInfo _mParse;
    private MethodInfo _mToString;
    private T _value;
    private bool _isPrimitive;

    public HexCnv(T v)
    {
        Type t = typeof(T);

        _isPrimitive = t.IsPrimitive;
        _value = v;
        _mParse = t.GetMethod("Parse", new Type[] { typeof(string), typeof(NumberStyles) });
        _mToString = t.GetMethod("ToString", new Type[] { typeof(string) });
    }

    public HexCnv(string v)
    {
        Type t = typeof(T);

        _isPrimitive = t.IsPrimitive;
        _mParse = t.GetMethod("Parse", new Type[] { typeof(string), typeof(NumberStyles) });
        _mToString = t.GetMethod("ToString", new Type[] { typeof(string) });
        FromString(v);
    }

    private void FromString(string s)
    {
        if (_isPrimitive && _mParse != null)
            _value = (T)_mParse.Invoke(null, new object[] { s.TrimStart('0', 'x'), NumberStyles.HexNumber });
    }

    public new string ToString()
    {
        string sOut = string.Empty;

        if (_isPrimitive) sOut = "0x"   _mToString.Invoke(_value, new object[] { "x2" }).ToString();
        return sOut;
    }

    public static implicit operator T(HexCnv<T> v) { return v._value; }

    public static implicit operator HexCnv<T>(T v) { return new HexCnv<T>(v); }

    public static implicit operator HexCnv<T>(string v) { return new HexCnv<T>(v); }
}

As you can see I changed the static methods, and made it works with simple assigning, see next snippet:

        HexCnv<short> mm = "0x1f55";

        Console.WriteLine("MM     = {0}", (int) mm);
        Console.WriteLine("MM-HEX = {0}", mm.ToString());

        //This produce the following result
        //  MM     = 8021
        //  MM-HEX = 0x1f55

        HexCnv<short> mm = "0x8f55"; //IDE show no errors on this, but
        HexCnv<short> mm = 0x8f55;   //It'll give an error on this expression,  
                                     //cuz the number is higher than "short"
  • Related