Java Integer Convert To convertIntArrayFromHex(char[] hex)

Here you can find the source of convertIntArrayFromHex(char[] hex)

Description

convert Int Array From Hex

License

Open Source License

Declaration

public static int convertIntArrayFromHex(char[] hex) 

Method Source Code

//package com.java2s;
/*//from  ww  w . j  a  va2s .c o m
 * aocode-public - Reusable Java library of general tools with minimal external dependencies.
 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2018, 2019  AO Industries, Inc.
 *     support@aoindustries.com
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aocode-public.
 *
 * aocode-public 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aocode-public 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aocode-public.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static int convertIntArrayFromHex(char[] hex) {
        int hexLen = hex.length;
        if (hexLen < 8)
            throw new IllegalArgumentException("Too few characters: " + hexLen);
        return (getHex(hex[0]) << 28) | (getHex(hex[1]) << 24) | (getHex(hex[2]) << 20) | (getHex(hex[3]) << 16)
                | (getHex(hex[4]) << 12) | (getHex(hex[5]) << 8) | (getHex(hex[6]) << 4) | (getHex(hex[7]));
    }

    /**
     * Converts one hex digit to an integer
     */
    public static int getHex(char ch) throws IllegalArgumentException {
        switch (ch) {
        case '0':
            return 0x00;
        case '1':
            return 0x01;
        case '2':
            return 0x02;
        case '3':
            return 0x03;
        case '4':
            return 0x04;
        case '5':
            return 0x05;
        case '6':
            return 0x06;
        case '7':
            return 0x07;
        case '8':
            return 0x08;
        case '9':
            return 0x09;
        case 'a':
        case 'A':
            return 0x0a;
        case 'b':
        case 'B':
            return 0x0b;
        case 'c':
        case 'C':
            return 0x0c;
        case 'd':
        case 'D':
            return 0x0d;
        case 'e':
        case 'E':
            return 0x0e;
        case 'f':
        case 'F':
            return 0x0f;
        default:
            throw new IllegalArgumentException("Invalid hex character: " + ch);
        }
    }
}

Related

  1. convertInt2Bytes(int data)
  2. convertInt32(int v, boolean isLE)
  3. convertInt8(int v)
  4. convertIntArray(int[] arr)
  5. convertIntArray(int[] in)
  6. convertIntBitsMSBinaryToIEEE(int source)
  7. convertIntColourToByteArray(int intColour)
  8. convertIntDouble(int[][] in)
  9. convertInteger(String input, int maxLen)