Java Char to Nibble charToNibble(char c)

Here you can find the source of charToNibble(char c)

Description

char To Nibble

License

Open Source License

Declaration

private static int charToNibble(char c) 

Method Source Code

//package com.java2s;
/*//from  w  w  w . j a  v  a  2s.  c  om
   Copyright 2005-2008, OW2 Aspire RFID project 
       
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Lesser General Public License as published by 
   the Free Software Foundation (the "LGPL"); either version 2.1 of the 
   License, or (at your option) any later version. If you do not alter this 
   notice, a recipient may use your version of this file under either the 
   LGPL version 2.1, or (at his option) any later version.
       
   You should have received a copy of the GNU Lesser General Public License 
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
       
   This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 
   KIND, either express or implied. See the GNU Lesser General Public 
   License for the specific language governing rights and limitations.
    
   Contact: OW2 Aspire RFID project <X AT Y DOT org> (with X=aspirerfid and Y=ow2)
    
   LGPL version 2.1 full text http://www.gnu.org/licenses/lgpl-2.1.txt    
*/

public class Main {
    private static int charToNibble(char c) {
        if ('0' <= c && c <= '9')
            return c - 48;
        if ('a' <= c && c <= 'f')
            return (c - 97) + 10;
        if ('A' <= c && c <= 'F')
            return (c - 65) + 10;
        else
            throw new IllegalArgumentException(
                    (new StringBuilder()).append("Invalid hex character: ").append(c).toString());
    }
}

Related

  1. charToNibble(char c)
  2. charToNibble(char c)
  3. charToNibble(char c)