Check if a character is Hex

G

Ghost

I am trying to determine if a string consists entirely of Hexidecimal
digits. Is there some way to check a string or a character to see if
it passes this criteria? I know in C, there is the isxdigit(char)
function.

The only solution I have found so far is to try to convert the string
to Hex and if there are any Exceptions thrown, then the string does not
contain all hex digits. I don't want to have to write my own function
although it would not be hard to do. Thanks for any advice.
 
T

Thomas Weidenfeller

Ghost said:
I am trying to determine if a string consists entirely of Hexidecimal
digits. Is there some way to check a string or a character to see if
it passes this criteria? I know in C, there is the isxdigit(char)
function.

There are many ways, e.g. by using a regexp, by using a loop and indeed
comparing characters for being within 0-9, a-f, A-F, by comparing
against Character.digit(char, 16) in a loop, etc.

/Thomas
 
G

Goran Novak

Ghost said:
I am trying to determine if a string consists entirely of Hexidecimal
digits. Is there some way to check a string or a character to see if
it passes this criteria? I know in C, there is the isxdigit(char)
function.

The only solution I have found so far is to try to convert the string
to Hex and if there are any Exceptions thrown, then the string does not
contain all hex digits. I don't want to have to write my own function
although it would not be hard to do. Thanks for any advice.

Maybe something like this:

public boolean isHexDigit(String hexDigit)
{
char[] hexDigitArray = hexDigit.toCharArray();
int hexDigitLength = hexDigitArray.length;

boolean isNotHex;
for (int i = 0; i < hexDigitLength; i++) {
isNotHex = Character.digit(hexDigitArray, 16) == -1;
if (isNotHex) {
return false;
}
}

return true;
}
 
Y

Yamin

There are many ways as people have talked about. looping through all
digits and checking for the ranges. But probably the easiet thing to
do is follow your instinct.

boolean isHex(String str)
{
int test = 0;
try
{
test = Integer.parseInt(str);
}
catch(Exception e)
{
return false;
}
return true;
}

This should be fine unless your really in tight processing loop, then
you may not want to be doing the check via exceptions. I was almost
sure the Character class had something like this...but it seems like it
can only tell if its a decimal digit.

On the other hand, depending on the larger context on where this will
be used, u might not want to do the explicit check and just rely on
exception handling itself.

Yamin Bismilla
 
Y

Yamin

There are many ways as people have talked about. looping through all
digits and checking for the ranges. But probably the easiet thing to
do is follow your instinct.

boolean isHex(String str)
{
int test = 0;
try
{
test = Integer.parseInt(str);
}
catch(Exception e)
{
return false;
}
return true;
}

This should be fine unless your really in tight processing loop, then
you may not want to be doing the check via exceptions. I was almost
sure the Character class had something like this...but it seems like it
can only tell if its a decimal digit.

On the other hand, depending on the larger context on where this will
be used, u might not want to do the explicit check and just rely on
exception handling itself.

Yamin Bismilla
 
P

Patricia Shanahan

Ghost said:
I am trying to determine if a string consists entirely of Hexidecimal
digits. Is there some way to check a string or a character to see if
it passes this criteria? I know in C, there is the isxdigit(char)
function.

The only solution I have found so far is to try to convert the string
to Hex and if there are any Exceptions thrown, then the string does not
contain all hex digits. I don't want to have to write my own function
although it would not be hard to do. Thanks for any advice.

Should "-f" be accepted or rejected?

If you literally want all hex digits, it should be rejected, and you
should use a looping character check. If you want a string that
represents an integer in hex, it should be accepted.

Integer.parseInt("-f",16) returns -15 with no exception.

Patricia
 
K

Knute Johnson

Ghost said:
I am trying to determine if a string consists entirely of Hexidecimal
digits. Is there some way to check a string or a character to see if
it passes this criteria? I know in C, there is the isxdigit(char)
function.

The only solution I have found so far is to try to convert the string
to Hex and if there are any Exceptions thrown, then the string does not
contain all hex digits. I don't want to have to write my own function
although it would not be hard to do. Thanks for any advice.

String.matches("\\p{XDigit}+");
 
R

Roedy Green

I am trying to determine if a string consists entirely of Hexidecimal
digits. Is there some way to check a string or a character to see if
it passes this criteria? I know in C, there is the isxdigit(char)
function.

this is close, Return a boolean instead of an int.

/**
* convert a single char to corresponding nibble.
*
* @param c char to convert. must be 0-9 a-f A-F, no
* spaces, plus or minus signs.
*
* @return corresponding integer
*/
private static int charToNibble ( char c )
{
if ( '0' <= c && c <= '9' )
{
return c - '0';
}
else if ( 'a' <= c && c <= 'f' )
{
return c - 'a' + 0xa;
}
else if ( 'A' <= c && c <= 'F' )
{
return c - 'A' + 0xa;
}
else
{
throw new IllegalArgumentException ( "Invalid hex character: " +
c );
}
}

See http://mindprod.com/jgloss/hex.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top