0

My current code:

UInt32 example = Convert.ToUInt32(tb_address.Text, 16);

Problem is if I'm peeking into an address like this:

0x83e3ba3c + 0x15

toUint32 will obviously return an error and say no.

Anyway I could go about having operators handled in it?

Picture of my app to maybe further understand it

crthompson
  • 15,653
  • 6
  • 58
  • 80

2 Answers2

0

Just split the string by using the '+'. After that trim the results, parse them and add them together.

Florian
  • 5,918
  • 3
  • 47
  • 86
  • Yeah, not a bad idea; i was asking if its possible to do this any other way. Just going to be a bit of a bitch. Got to think how to go about doing that. – user3009127 Nov 19 '13 at 14:26
0

Probably your best option would be to hide the code in a static class. something like this should work:

public static class HexAdder
{
    private static UInt32 num1 = 0, num2 = 0;
    private static void ParseString(string stringtoadd)
    {
        string[] temp = {"",""};
        try
        {
            temp = stringtoadd.Split(" +".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            num1 = Convert.ToUInt32(temp[0], 16);
            num2 = Convert.ToUInt32(temp[1], 16);
        }

        catch(Exception e)
        {

            MessageBox.Show("Invalid String Format\n- Added String = " + stringtoadd + "\n- First Part = " + temp[0] + "\n- Second Part = " + temp[1]);
        }
    }
    public static UInt32 AddedToUint32(string stringtoadd)
    {
        ParseString(stringtoadd);
        return num1 + num2;
    }
}

This has basic validation and pops up a messagebox and returns 0 if there is an error.

Using it would look like this:

        string test = "0x83e3ba3c + 0x15";
        UInt32 example = HexAdder.AddedToUint32(test);

This way adding other operations is a simple matter of adding the appropriate static method(s).

tinstaafl
  • 6,908
  • 2
  • 15
  • 22