Test a string for Hex or Oct

J

Joshua Flanagan

The simplest way to test would probably be with a regular expression.

using System.Text.RegularExpressions;
....

// to detect an oct number
Regex.IsMatch(givenString, "[0-7]+");
// to detect a hex number
Regex.IsMatch(givenString, "[0-9A-Fa-f]+");

You can also use Int32.Parse() or Decimal.Parse() to convert a hex
number to a decimal integer.


Joshua Flanagan
http://flimflan.com/blog
 
G

Guest

Hi MAF:

using System;
using System.Text.RegularExpressions;

class MainApp
{
public static void Main()
{
string str1 = "DEADBEEF";

string strOct = String.Concat("[0-7]{", str1.Length, "}");
bool RetBoolOct = Regex.IsMatch(str1, strOct);
Console.WriteLine(RetBoolOct);

string strHex = String.Concat("[0-9A-Fa-f]{", str1.Length, "}");
bool RetBoolHex = Regex.IsMatch(str1, strHex);
Console.WriteLine(RetBoolHex);
}
}



Joshua Flanagan said:
The simplest way to test would probably be with a regular expression.

using System.Text.RegularExpressions;
....

// to detect an oct number
Regex.IsMatch(givenString, "[0-7]+");
// to detect a hex number
Regex.IsMatch(givenString, "[0-9A-Fa-f]+");

You can also use Int32.Parse() or Decimal.Parse() to convert a hex
number to a decimal integer.


Joshua Flanagan
http://flimflan.com/blog
How can I test a given string to see if the string is a hex number or an oct
number?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top