473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Test a string for Hex or Oct

MAF
How can I test a given string to see if the string is a hex number or an oct
number?
Nov 17 '05 #1
2 7174
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

MAF wrote:
How can I test a given string to see if the string is a hex number or an oct
number?

Nov 17 '05 #2
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" wrote:
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

MAF wrote:
How can I test a given string to see if the string is a hex number or an oct
number?

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kamilche | last post by:
I was looking for a way to speed up detecting invalid characters in my TCP string, and thought of yet another use for the translate function! If you were to 'translate out' the bad characters, and...
12
by: Nollie | last post by:
I need to write a couple of my own string manipulation routines (e.g. a strcpy() alternative that returns the number of chars copied). I've started with one of the simpler functions, strlen(). I've...
6
by: Thomas Moore | last post by:
Hi: I am confused at string identity test: Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a="test" >>>...
4
by: Benne Smith | last post by:
In our company, i have three servers; 1) a development server (mine only - here i make daily changes and test my stuff) 2) a test server (for the users to test milestone builds - changes weekly)...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
13
by: joenuts | last post by:
Is it possible for a function to test one of it's passed in variables (reference to object) for validity? I would like the displayString( string &obString) function to verify that obString 1)...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.