Java Hex Convert To fromHex(String input, int max)

Here you can find the source of fromHex(String input, int max)

Description

Converts a string from hex to integer.

License

Open Source License

Parameter

Parameter Description
input String
max Maximum permitted value

Exception

Parameter Description
IllegalArgumentException If it's invalid or out of range

Return

Number

Declaration

private static int fromHex(String input, int max) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*//from w ww .j av a  2 s  .c o  m
This file is part of leafdigital browserstats.
    
browserstats is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
browserstats is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with browserstats.  If not, see <http://www.gnu.org/licenses/>.
    
Copyright 2010 Samuel Marshall.
*/

public class Main {
    /**
     * Converts a string from hex to integer.
     * @param input String
     * @param max Maximum permitted value
     * @return Number
     * @throws IllegalArgumentException If it's invalid or out of range
     */
    private static int fromHex(String input, int max) throws IllegalArgumentException {
        try {
            int value = Integer.parseInt(input.toLowerCase(), 16);
            if (value < 0 || value > max) {
                throw new IllegalArgumentException("Out of range: " + input);
            }
            return value;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Not valid hex: " + input);
        }
    }
}

Related

  1. fromHex(String hexBytes)
  2. fromHex(String hexData)
  3. fromHex(String hexStr)
  4. fromHex(String hexString)
  5. fromHex(String hexString)
  6. fromHex(String s)
  7. fromHex(String s)
  8. fromHex(String s)
  9. fromHex(String s)