Check if a character is Hex

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

  1. Ghost

    Ghost Guest

    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
    #1
    1. Advertising

  2. 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/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Jun 29, 2005
    #2
    1. Advertising

  3. Ghost

    Goran Novak Guest

    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
    #3
  4. Ghost

    Yamin Guest

    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, Jun 29, 2005
    #4
  5. Ghost

    Yamin Guest

    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, Jun 29, 2005
    #5
  6. 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
    #6
  7. 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
    #7
  8. "Yamin" <> writes:

    > test = Integer.parseInt(str);


    This uses a base 10 as default, you need to specify radix.
     
    Tor Iver Wilhelmsen, Jun 29, 2005
    #8
  9. Ghost

    Roedy Green Guest

    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

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jun 30, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,269
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    485
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    596
  4. Replies:
    3
    Views:
    503
    James Kanze
    Nov 18, 2007
  5. Replies:
    7
    Views:
    3,634
Loading...

Share This Page