Java Hex Convert To convertHexLong(String hex)

Here you can find the source of convertHexLong(String hex)

Description

Convert hex to long.

License

Open Source License

Parameter

Parameter Description
hex a string.

Return

a long.

Declaration

public static long convertHexLong(String hex) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Liviu Ionescu.//w  ww .  j  a  v a2s.c  o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Liviu Ionescu - initial version
 *******************************************************************************/

public class Main {
    /**
     * Convert hex to long. Considers +/-, ignores 0x and 0X.
     * 
     * @param hex
     *            a string.
     * @return a long.
     */
    public static long convertHexLong(String hex) {

        boolean isNegative = false;
        if (hex.startsWith("+")) {
            hex = hex.substring(1);
        } else if (hex.startsWith("-")) {
            hex = hex.substring(1);
            isNegative = true;
        }

        if (hex.startsWith("0x") || hex.startsWith("0X")) {
            hex = hex.substring(2);
        }

        long value = Long.valueOf("0" + hex, 16);
        if (isNegative)
            value = -value;

        return value;
    }
}

Related

  1. convertHexadecimal2RGB(final String hexColor, final String divider)
  2. convertHexByteToTelcoChar(byte byteValue)
  3. convertHexChars(final String value)
  4. convertHexColorToRgb(final String hex)
  5. convertHexFloatingPointLiteralToBits(char[] source)
  6. convertHexStringToBinary(String hexString)
  7. convertHexTime2Binary(String timespan)
  8. convertHextoASCII(String text)
  9. convertHexToChar(String hex)