Click here to Skip to main content
12,634,171 members (26,858 online)
Rate this:
Please Sign up or sign in to 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 16-May-12 7:55am
SAKryukov 16-May-12 13:15pm
It looks like this is .NET, so must be C++/CLI, not C++. Tag it: "C++", ".NET".
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 16 May 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100