Thread: First Windows Program

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    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.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    76
    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?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dolfaniss View Post
    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...

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    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!
    Last edited by dolfaniss; 02-17-2011 at 08:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why .exe file is not working in windows of c program
    By suryak in forum C Programming
    Replies: 9
    Last Post: 01-07-2011, 12:23 PM
  2. Program works on Windows XP and 2000, not on 98 or ME
    By MidnightlyCoder in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 03:36 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. my first windows console program
    By Syneris in forum Windows Programming
    Replies: 3
    Last Post: 04-08-2002, 03:18 PM