Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Check if a character is Hex

Reply
Thread Tools

Check if a character is Hex

 
 
Ghost
Guest
Posts: n/a
 
      06-29-2005
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.

 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      06-29-2005
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:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
 
 
 
Goran Novak
Guest
Posts: n/a
 
      06-29-2005
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[i], 16) == -1;
if (isNotHex) {
return false;
}
}

return true;
}
 
Reply With Quote
 
Yamin
Guest
Posts: n/a
 
      06-29-2005
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

 
Reply With Quote
 
Yamin
Guest
Posts: n/a
 
      06-29-2005
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

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      06-29-2005
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


 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      06-29-2005
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/
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      06-29-2005
"Yamin" <(E-Mail Removed)> writes:

> test = Integer.parseInt(str);


This uses a base 10 as default, you need to specify radix.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-30-2005
On 29 Jun 2005 07:20:17 -0700, "Ghost" <(E-Mail Removed)> 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/...s_rumsfeld.htm

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UTF-8 Character Encodings and "NO-BREAK SPACE" (dec: 202, hex: CA)Character mrdecav@gmail.com HTML 7 02-01-2009 11:05 PM
check and convert lower character to upper character alessio211734@yahoo.it C++ 3 11-18-2007 11:28 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments