# First Windows Program

• 02-16-2011
dolfaniss
First Windows Program
How do I convert user input, into a textbox to hex? This is my first windows program. I realize I have a lot to learn and this is probably over my head but would like to finish my program. It is not homework, it's just something that could be a little useful at my job. Here is the code for my caculate button:

Code:

```#pragma endregion         private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {                         int start_cyl = System::Int32::Parse(textBox1->Text);                         int end_cyl = System::Int32::Parse(textBox2->Text);                         int start_head = System::Int32::Parse(textBox4->Text);                         int end_head = System::Int32::Parse(textBox5->Text);                         int ans = (start_cyl * 0x80 * 0xf) + (start_head * 0x80);                         int ans1 = (end_cyl * 0x80 * 0xf) + (end_head * 0x80);                         textBox6->Text = ans.ToString();                         textBox3->Text = ans1.ToString();```
I need to convert textboxes 1, 2 4 and 5 to hex to get the calculation right. Do I do that from here or can I specify in the textbox code, which I haven't touched?

Any help would be appreciated.
• 02-16-2011
CommonTater
Why do you need to convert it to hex?

Once you have it as an integer, it's all binary no matter what number base it's entered in...

Code:

```int x; x = 10; x = x * 0x0f;```
Is perfectly legitmate and will return 150, which is the correct answer.
• 02-16-2011
dolfaniss
Thanks, I'm not sure that helps me 100%. I need to be able to acccept hex numbers from the user. With this method I'd be taking a decimal number than converting it?
• 02-16-2011
CommonTater
Quote:

Originally Posted by dolfaniss
Thanks, I'm not sure that helps me 100%. I need to be able to acccept hex numbers from the user. With this method I'd be taking a decimal number than converting it?

Again there's no reason to convert it once you have it in an int since the int is binary storage.

Getting user entry in hex is a matter for taking it as a character string from the edit control then converting the string from hex to an integer. In C it's done like this...
Code:

```char HexStr = "FE00A7";  // for example int DecInt; sscanf(HexStr,"%x",&DecInt); // display printf("%d", DecInt);```
I didn't take much note of the language you're working in... but I'm sure it has a similar function.

As another option there are custom controls, available as libraries, that do this for you internally...
• 02-16-2011
dolfaniss
Code:

```#pragma endregion         private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {                         int start_cyl = Int32::Parse(textBox1->Text, NumberStyles::HexNumber);                         int end_cyl = Int32::Parse(textBox2->Text, NumberStyles::HexNumber);                         int start_head = System::Int32::Parse(textBox4->Text, NumberStyles::HexNumber);                         int end_head = System::Int32::Parse(textBox5->Text, NumberStyles::HexNumber);                         int ans = (start_cyl * 0x80 * 0xf) + (start_head * 0x80);                         int ans1 = (end_cyl * 0x80 * 0xf) + (end_head * 0x80);                         textBox6->Text = ans.ToString();                         textBox3->Text = ans1.ToString();```
This did the trick!