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

x
Solved

# Is it a Hex Value?

Posted on 2012-08-30
Medium Priority
489 Views
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?

0
Question by:dashifire
• 4
• 4
• 2
• +1

LVL 37

Expert Comment

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

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.

0

Author Comment

ID: 38351073
I just want to make sure the value isn't like a G or Z, is that possible?
0

LVL 99

Accepted Solution

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.

0

Author Closing Comment

ID: 38351092
Rats, I was afraid of that... Thanks!
0

LVL 99

Expert Comment

ID: 38351140
Thanks and good luck going forward with this. .... Thinkpads_User
0

LVL 86

Expert Comment

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!");
}
}
``````

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

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

LVL 86

Expert Comment

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

Author Comment

ID: 38351289
Thanks Idle_Mind! It worked perfectly

``````bool isHex = true;
try
{
int decValue = Convert.ToInt32(textBox1.Text, 16);
}
catch
{
isHex = false;
}
``````
my if statement became
``````if (textBox1.Text.Length == 6&&isHex)
``````
0

LVL 99

Expert Comment

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

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
Course of the Month17 days, 14 hours left to enroll