Check a string to see if all characters are hexadecimal values
public bool OnlyHexInString(string test)
{
// For C-style hex notation (0xFF) you can use @"\A\b(0[xX])?[0-9a-fA-F]+\b\Z"
return System.Text.RegularExpressions.Regex.IsMatch(test, @"\A\b[0-9a-fA-F]+\b\Z");
}
Check if string is valid representation of hex number
I would have thought that it's quickest to attempt to convert your string to an integral type and deal with any exception. Use code like this:
int num = Int32.Parse(s, System.Globalization.NumberStyles.HexNumber);
The resulting code is possibly easier to follow than a regular expression and is particularly useful if you need the parsed value (else you could use Int32.TryParse
which is adequately documented in other answers).
(One of my favourite quotations is by Jamie Zawinski: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems.")
Check if a string is hexadecimal
(1) Using int() works nicely for this, and Python does all the checking for you :)
int('00480065006C006C006F00200077006F0072006C00640021', 16)
6896377547970387516320582441726837832153446723333914657L
will work. In case of failure you will receive a ValueError
exception.
Short example:
int('af', 16)
175
int('ah', 16)
...
ValueError: invalid literal for int() with base 16: 'ah'
(2) An alternative would be to traverse the data and make sure all characters fall within the range of 0..9
and a-f/A-F
. string.hexdigits
('0123456789abcdefABCDEF'
) is useful for this as it contains both upper and lower case digits.
import string
all(c in string.hexdigits for c in s)
will return either True
or False
based on the validity of your data in string s
.
Short example:
s = 'af'
all(c in string.hexdigits for c in s)
True
s = 'ah'
all(c in string.hexdigits for c in s)
False
Notes:
As @ScottGriffiths notes correctly in a comment below, the int()
approach will work if your string contains 0x
at the start, while the character-by-character check will fail with this. Also, checking against a set of characters is faster than a string of characters, but it is doubtful this will matter with short SMS strings, unless you process many (many!) of them in sequence in which case you could convert stringhexditigs to a set with set(string.hexdigits)
.
How can I test whether a string contains only hex characters in C#?
You're misunderstanding the Multiline
option:
Use multiline mode, where ^ and
$ match the beginning and end of each line (instead of the beginning
and end of the input string).
Change it to
static readonly Regex r = new Regex(@"^[0-9A-F\r\n]+$");
public static bool VerifyHex(string _hex)
{
return r.Match(_hex).Success;
}
How can we efficiently check if a string is hexadecimal in Python
int(s, 16)
will still have complexity O(n), where n == len(s)
, but the two aren't directly comparable. int
will be iterating over the data at a lower level than all
, which is faster, but int
also does more work (it actually has to compute the integer value of s
).
So which is faster? You have to profile both.
In [1]: s = "783c"
In [2]: import string
In [3]: %timeit all(c in string.hexdigits for c in s)
800 ns ± 3.23 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [4]: %%timeit
...: try:
...: int(s, 16)
...: except ValueError:
...: pass
...:
223 ns ± 1.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Looks like internal iteration wins. I tested on an 9-digit string as well, and int
was still about 4x faster.
But what about invalid strings?
In [8]: s = 'g'
In [9]: %%timeit
...: try:
...: int(s, 16)
...: except ValueError:
...: pass
...:
1.09 µs ± 2.62 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [10]: %timeit all(c in string.hexdigits for c in s)
580 ns ± 6.55 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Now, we're basically testing the benefit of short-circuiting versus the cost of catching an exception. What happens if the error occurs later in the string?
In [11]: s = "738ab89ffg"
In [12]: %timeit all(c in string.hexdigits for c in s)
1.59 µs ± 19.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [13]: %%timeit
...: try:
...: int(s, 16)
...: except ValueError:
...: pass
...:
1.25 µs ± 19.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Now we're seeing the benefit to internal iteration again.
Regex to check string contains only Hex characters
Yes, you can do that with a regular expression:
^[0-9A-F]+$
Explanation:
^ Start of line.
[0-9A-F] Character class: Any character in 0 to 9, or in A to F.
+ Quantifier: One or more of the above.
$ End of line.
To use this regular expression in Java you can for example call the matches
method on a String:
boolean isHex = s.matches("[0-9A-F]+");
Note that matches
finds only an exact match so you don't need the start and end of line anchors in this case. See it working online: ideone
You may also want to allow both upper and lowercase A-F, in which case you can use this regular expression:
^[0-9A-Fa-f]+$
Checking to see if a charArray has only hex digits
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <errno.h>
bool isValid1(const char *s){
unsigned long v;
char *endp;
errno = 0;
v = strtoul(s, &endp, 16);
return !*endp && v < 0x10000 && errno != ERANGE;
}
bool isValid2(const char *s){//limited to four HEX characters : 0xXXXX
if(strlen(s)!=6)
return false;
return
*s == '0' && tolower(s[1])=='x' &&
isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4]) && isxdigit(s[5]);
}
int main(void){
char data[] =
"0xFFFF 0x0000 0x1234 0x01234 $0xFFFF 0x45GG 0x12345\n";
char *p = data;
for(p = strtok(p, " \n"); p ; p = strtok(NULL, " \n")){
if(!isValid1(p))
printf("isValid1(%s) : invalid input\n", p);
if(!isValid2(p))
printf("isValid2(%s) : invalid input\n", p);
}
return 0;
}
Ruby, check if string is all valid hex characters?
In ruby regex \h matches a hex digit and \H matches a non-hex digit.
So!str[/\H/]
is what you're looking for.
Related Topics
Is There a Tool for Finding Unreferenced Functions (Dead, Obsolete Code) in a C# App
Why Does a Float Variable Stop Incrementing at 16777216 in C#
Is Correct to Use Gc.Collect(); Gc.Waitforpendingfinalizers();
Entity Framework Specification Pattern Implementation
Spawn a New Thread to Open a New Window and Close It from a Different Thread
Block Request for Multiple Unsuccessful Logins for a Period of Time
Htmlagilitypack and Dynamic Content Issue
How to Change The Colour of The Line Below/Border of a Textbox (Entry)
Recursive Linq Query: Select Item and All Children with Subchildren
Does C# Have a Way of Giving Me an Immutable Dictionary
How to Get All the Possible 3 Letter Permutations
Why Must C# Operator Overloads Be Static
How to Check If a Given Value Is a Generic List
How to Add a Reference to an Unmanaged C++ Project Called by a C# Project
How to Get Error Information When Httpwebrequest.Getresponse() Fails