# Check if a character is Hex

Discussion in 'Java' started by Ghost, Jun 29, 2005.

1. ### GhostGuest

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.

Ghost, Jun 29, 2005

2. ### Thomas WeidenfellerGuest

Ghost wrote:
> 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

--
The comp.lang.java.gui FAQ:
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Thomas Weidenfeller, Jun 29, 2005

3. ### Goran NovakGuest

Ghost wrote:
> 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;
}

Goran Novak, Jun 29, 2005
4. ### YaminGuest

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

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

Yamin, Jun 29, 2005
5. ### YaminGuest

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

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

Yamin, Jun 29, 2005
6. ### Patricia ShanahanGuest

Ghost wrote:
> 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

Patricia Shanahan, Jun 29, 2005
7. ### Knute JohnsonGuest

Ghost wrote:

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

--

Knute Johnson
email s/nospam/knute/

Knute Johnson, Jun 29, 2005
8. ### Tor Iver WilhelmsenGuest

"Yamin" <> writes:

> test = Integer.parseInt(str);

This uses a base 10 as default, you need to specify radix.

Tor Iver Wilhelmsen, Jun 29, 2005
9. ### Roedy GreenGuest

On 29 Jun 2005 07:20:17 -0700, "Ghost" <> wrote
or quoted :

>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