Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello everybody!

My project is windows form application (using Microsoft Visual C++ Studio 2010).
I've created a textBox. There are 3 radio buttons below that textBox (named Hex, Dec, Bin). What I need is after the user chooses one of the option given by clicking on a radio button, he enters some number (let's say he chooses Bin and enters 11100). Then he clicks on Dec the number in textBox will be changed to 28 (the decimal value of 11100). If he clicks on Hex then the number will be changed to 1C (the hexcimal of 11100).

Now is the point: let's assume that the user chooses Hex and enters 1C. After I retrieve that text as a string, I have to declare a variable of type DWORD like DWORD d; that holds the hexcimal value of the text. How could I do that because I don't know what number the user is going to enter. If I knew that he is going to enter 1C, then I could declare like this DWORD d = 0x1C.

Please help me with this!

Any help would be appreciated!

Thank in advance!
Posted
Comments
Sergey Alexandrovich Kryukov 16-May-12 13:15pm    
It looks like this is .NET, so must be C++/CLI, not C++. Tag it: "C++", ".NET".
--SA

You have a problem because the concept is wrong in the very basics. The variable declared as DWORD cannot be decimal, or hexadecimal; it's binary. Only the string representing numbers can be. Also, declaration of variable is the compile-time thing, so you cannot declare or even initialize one or another in response to the some action.

You are trying to make is complex because you don't understand simple things. You simply need to parse some string into only one type you are going to use. Use System::UInt32::Parse or System::UInt32::TryParse. Allow the user to enter anything (you can filter out unwanted characters though handling System.Windows.Forms::Control::KeyPress and parse the text to uint only when you need to use the value.

—SA
 
Share this answer
 
v2
You must write parsers that convert the text to a number according to the actual number base selection and functions that create a text string representing the number according to the clicked number base button.

To convert text to numbers, have a look at the C function strol() where you can specify the number's base.

To create text from decimal and hex numbers, use the format specifications of C style printf() functions. Creating a text string with binary representation must be done by yourself (but it's very simple).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900