Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Check if a character is Hex

Thread view: 
Ghost - 29 Jun 2005 15:20 GMT
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.
Thomas Weidenfeller - 29 Jun 2005 15:45 GMT
> 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

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Goran Novak - 29 Jun 2005 16:04 GMT
> 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
[quoted text clipped - 5 lines]
> 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;
}
Yamin - 29 Jun 2005 16:22 GMT
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
Yamin - 29 Jun 2005 16:26 GMT
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
Tor Iver Wilhelmsen - 29 Jun 2005 21:36 GMT
>      test = Integer.parseInt(str);

This uses a base 10 as default, you need to specify radix.
Patricia Shanahan - 29 Jun 2005 16:47 GMT
> 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
[quoted text clipped - 5 lines]
> 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
Knute Johnson - 29 Jun 2005 17:29 GMT
> 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
[quoted text clipped - 5 lines]
> 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}+");

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 30 Jun 2005 07:21 GMT
>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

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.