Parses the string argument as a signed long with the radix 16 - Java java.lang

Java examples for java.lang:long Parse

Description

Parses the string argument as a signed long with the radix 16

Demo Code

import java.util.Random;

public class Main{

    public static void main(String[] argv){
        String str = "dead";
        System.out.println(parseUnsignedLong(str));
    }/*from   w w  w .  j a v  a 2s. c o m*/
    /**
     * Parses the string argument as a signed long with the radix 16.
     * 
     * @param str
     *            the string to parse
     * @return the long represented by the argument in decimal.
     * @throws NumberFormatException
     *             if the string does not contain a parsable long.
     */
    public static long parseUnsignedLong(String str)
            throws NumberFormatException {
        if (str.length() > 16) {
            throw new NumberFormatException();
        }
        int lowstart = str.length() - 8;
        if (lowstart <= 0)
            return Long.parseLong(str, 16);
        else
            return Long.parseLong(str.substring(0, lowstart), 16) << 32
                    | Long.parseLong(str.substring(lowstart), 16);
    }

}

Related Tutorials