How do you display a number in textbox as Hex value.  
Author Message
FreddieCode





PostPosted: Visual C# Language, How do you display a number in textbox as Hex value. Top

 

I have 3 text boxes.  The user will enter hex numbers in textBox1 and textBox2.  I want to add them together and display the HEX sum in textBox3.

This code ALMOST works.  It displays the answer in DECIMAL.  How can I display it in HEX   Thanks.

int mynum1;

int mynum2;

int myanswer;

mynum1 = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber);

mynum2 = Int32.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber);

myanswer = mynum1 + mynum2;

textBox3.Text = myanswer.ToString();   //this displays in Decimal.  How to Display in hex




Visual C#17  
 
 
Mike Hildner





PostPosted: Visual C# Language, How do you display a number in textbox as Hex value. Top

textBox3.Text = myanswer.ToString("X") - or "x" for lower case.

Check out "standard numeric format strings" in MSDN.


 
 
SvenC





PostPosted: Visual C# Language, How do you display a number in textbox as Hex value. Top

Use a format string:

myanswer.ToString("x");

--
SvenC


 
 
FreddieCode





PostPosted: Visual C# Language, How do you display a number in textbox as Hex value. Top

I was SO close. I was doing the following, which always prints 2X in the textbox.

textBox3.Text = myanswer.ToString("2X"); //this is not correct!!!

Thanks for your help guys!