Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how to validate range of hex,binary,octal or decimal numbers?

VashudevVashudev Posts: 28Member
Hi everybody!
I am needing to validate the numbers entered by the user. The user enters number in either of the four formats namely hex,binary,octal or decimal. I need to validate the numbers such that if the user enters a hex number other than 0 to 7FFFFFFF my if statement should be able to validate that and prompt the user again to enter the number in range. In the same way if a binary number is entered other than
0 to 1111111111111111111111111111111(31 1's) or an octal number other than 0 to 17777777777, again my if statement or any other mechanism should be able to validate and output a warning message about out of range number and prompt the user to enter that number in question again. I just can't start thinking in some direction...please help.
Thanks in advance.
vashudev chandra

Comments

  • arb123arb123 Posts: 696Member
    See methods like [b]Integer.parseInt(String val, int radix)[/b] and [b]Integer.toString(int val, int radix)[/b].

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • VashudevVashudev Posts: 28Member
    Hi!
    Thanks for yoru previous reply but it is not the problem in converting the number. I need to know the mechanism of compare. Say a user enters 177777 in hex format...now i need to make sure that the hex number entered is in the range 0 to 17777777777(10 7's) and I also need to make sure that the number is not a negative or >17777777777.
    Converting from one format to another is not a problem at all for me.
    I am reading the input by user in something like this str.readLine();
    Now I want to pass the str to a function which will basically validate if a particular number format is in particular range and specification or not.
    Thanks again!
    Vahudev

    See methods like [b]Integer.parseInt(String val, int radix)[/b] and [b]Integer.toString(int val, int radix)[/b].
    :
    : ---------------------------------
    : [size=1](Its just my sig)
    : HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :



  • arb123arb123 Posts: 696Member
    : Hi!
    : Thanks for yoru previous reply but it is not the problem in converting the number. I need to know the mechanism of compare. Say a user enters 177777 in hex format...now i need to make sure that the hex number entered is in the range 0 to 17777777777(10 7's) and I also need to make sure that the number is not a negative or >17777777777.
    : Converting from one format to another is not a problem at all for me.
    : I am reading the input by user in something like this str.readLine();
    : Now I want to pass the str to a function which will basically validate if a particular number format is in particular range and specification or not.
    : Thanks again!
    : Vahudev

    Use the functions I mentioned to convert to a numeric format (int or long). Then compare that number to your range. This is much simpler than attempting to do it using chars.

    [code]
    String value = "1F";

    final long min = 0x0;
    final long max = 0xFF;

    long n = Long.parseLong(value, 16);

    boolean ok = (n >= min) && (n <= max);
    System.out.println(ok);
    [/code]

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • VashudevVashudev Posts: 28Member
    Hello there!
    Thanks a lot for your help. The last reply by you was very very helpful...I don't know how many hours I would have lost by trying to look that up myself...it works. EUREKA! I am new at java and I have strong feelings for it. This is my life, my future. So please keep your helping hand extended to me. I appreciate everybody at Programmers Heaven.com. I will be posting more questions and please keep replying as you always do.
    Thanks
    Vasudev

    : : Hi!
    : : Thanks for yoru previous reply but it is not the problem in converting the number. I need to know the mechanism of compare. Say a user enters 177777 in hex format...now i need to make sure that the hex number entered is in the range 0 to 17777777777(10 7's) and I also need to make sure that the number is not a negative or >17777777777.
    : : Converting from one format to another is not a problem at all for me.
    : : I am reading the input by user in something like this str.readLine();
    : : Now I want to pass the str to a function which will basically validate if a particular number format is in particular range and specification or not.
    : : Thanks again!
    : : Vahudev
    :
    : Use the functions I mentioned to convert to a numeric format (int or long). Then compare that number to your range. This is much simpler than attempting to do it using chars.
    :
    : [code]
    : String value = "1F";
    :
    : final long min = 0x0;
    : final long max = 0xFF;
    :
    : long n = Long.parseLong(value, 16);
    :
    : boolean ok = (n >= min) && (n <= max);
    : System.out.println(ok);
    : [/code]
    :
    : ---------------------------------
    : [size=1](Its just my sig)
    : HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :

Sign In or Register to comment.