[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Is it a Hex Value?

Posted on 2012-08-30
11
Medium Priority
?
489 Views
Last Modified: 2012-08-30
I am trying to check if a value from a textBox is a Hex value. Is there a simple way to do this so that I can put it in an if statement?

Thanks in advance,
0
Comment
Question by:dashifire
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 38351024
No.

What happens if they enter 99 ?
Is that 99 or 0x99 ?

You could insist that all entries are proceeded with 0x IF they are to be treated as HEX
0
 
LVL 99

Expert Comment

by:John Hurst
ID: 38351063
Agree with the above - you must have a way to declare it in the text box.

0, 1, 2, 3, 4, 5, 6, and 7 are all the same:  Decimal, Octal and Hexadecimal. They are indistinguishable.

.... Thinkpads_User
0
 

Author Comment

by:dashifire
ID: 38351073
I just want to make sure the value isn't like a G or Z, is that possible?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 99

Accepted Solution

by:
John Hurst earned 2000 total points
ID: 38351086
You would have to screen the box for legitimate characters (0-9 and A-F). You would have to write the code up front to do this in the box if you do not wish to entertain errors when reading the data. But it is a coding exercise.

.... Thinkpads_User
0
 

Author Closing Comment

by:dashifire
ID: 38351092
Rats, I was afraid of that... Thanks!
0
 
LVL 99

Expert Comment

by:John Hurst
ID: 38351140
Thanks and good luck going forward with this. .... Thinkpads_User
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38351154
I disagree...you don't have to write this from scratch!

Just use Convert.ToInt32() and specify base 16:
        private void button1_Click(object sender, EventArgs e)
        {
            try 
	        {
                string hexCandidate = textBox1.Text;
                int decValue = Convert.ToInt32(hexCandidate, 16);
                label1.Text = hexCandidate + " (hexadecimal) = " + decValue.ToString() + " (decimal)";
	        }
	        catch (Exception)
	        {
                MessageBox.Show(textBox1.Text, "Not a valid hexadecimal number!");
	        }
        }

Open in new window


This works for strings such as "0xF2" too.

See Convert.ToInt32():
http://msdn.microsoft.com/en-us/library/1k20k614.aspx

    "If fromBase is 16, you can prefix the number specified by the value parameter with "0x" or "0X"."
0
 

Author Comment

by:dashifire
ID: 38351264
Giving that a shot! crap, how do i give you credit if this works?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38351269
You don't.   One doesn't post in CLOSED questions if they are looking for points...  ;)
0
 

Author Comment

by:dashifire
ID: 38351289
Thanks Idle_Mind! It worked perfectly

bool isHex = true;
                    try
                    {
                        int decValue = Convert.ToInt32(textBox1.Text, 16);
                    }
                    catch
                    {
                        isHex = false;
                    }

Open in new window

my if statement became
if (textBox1.Text.Length == 6&&isHex)

Open in new window

0
 
LVL 99

Expert Comment

by:John Hurst
ID: 38351375
Dashifire - if you wish to Request Attention and have a Moderator split the points that is fine with me. .... Thinkpads_User
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question