Java Long Number Create convertLongArrayFromHex(char[] hex)

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

Description

convert Long Array From Hex

License

Open Source License

Declaration

public static long convertLongArrayFromHex(char[] hex) 

Method Source Code

//package com.java2s;
/*/*from w w  w  .j a va 2 s .c om*/
 * 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 long convertLongArrayFromHex(char[] hex) {
        int hexLen = hex.length;
        if (hexLen < 16)
            throw new IllegalArgumentException("Too few characters: " + hexLen);
        int h = (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]));
        int l = (getHex(hex[8]) << 28) | (getHex(hex[9]) << 24) | (getHex(hex[10]) << 20) | (getHex(hex[11]) << 16)
                | (getHex(hex[12]) << 12) | (getHex(hex[13]) << 8) | (getHex(hex[14]) << 4) | (getHex(hex[15]));
        return (((long) h) << 32) | (l & 0xffffffffL);
    }

    /**
     * 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. convertLong(byte[] data, int offset)
  2. convertLong(long v, boolean isLE)
  3. convertLong2FourBytes(long data)
  4. convertLongAddressToBuf(long ipAddress)
  5. convertLongArray(long[] arr)
  6. convertLongArrayToDoubleArray(long[] values)
  7. convertLongFromBytes(byte[] bytes)
  8. convertLongitudeFromMercator( double mercatorLongitude)
  9. convertLongPriceToString(long price)