Question

I need help to divide hex characters something like this. I know this does not work. If i enter 418 my answer should be 20C. Im using C# Thanks

private void button23_Click(object sender, EventArgs e)  //calculator
    {

        {
            offset = textBox10.Text;

           offset hex / 2 = offset2


            textBox11.Text = offset2;

        }
    }
Était-ce utile?

La solution

NumberStyles is a bit esoteric but it's how you do it:

int offset = int.Parse(textBox10.Text, System.Globalization.NumberStyles.HexNumber);
int offset2 = offset / 2;
textBox11.Text = String.Format("{0:X}", offset2);

Of course, you might need more processing and validation of the user input. Look at int.TryParse and different int sizes (e.g. Int64), too.

Autres conseils

You can use int.Parse to convert your hex string to an int, perform any arithmetic operation you require on it, then use ToString to convert it back to a hex string.

string offset = textBox10.Text;
int val = int.Parse(offset, System.Globalization.NumberStyles.HexNumber);
int div = val / 2;
string offset2 = div.ToString("X");
textBox11.Text = offset2;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top