Java Long to Hex longToHex(long num)

Here you can find the source of longToHex(long num)

Description

A quick method to convert num to a hexadecimal number.

License

Open Source License

Parameter

Parameter Description
num The number to convert

Declaration

public static String longToHex(long num) 

Method Source Code

//package com.java2s;
/* This file is part of Math4J.
 * Math4J is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.//from   ww w . ja  v  a 2s . c  o  m
 *
 * Math4J 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Math4J; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * 
 * Copyright 2005 Anthony Magee
 */

public class Main {
    /**
     * A quick method to convert <code>num</code> to a hexadecimal number. A
     * hexadecimal number contains the digits zero through nine and the first
     * five letters of the english alphabet (A, B, C, D, E, F).
     * 
     * @param num The number to convert
     */
    public static String longToHex(long num) {
        /*
         * StringBuffer returnBuf = new StringBuffer(17);
         * 
         * long power = 0; while(num > (long) Math.pow(16, power)) { power++; }
         * 
         * for(long i = power - 1; i >= 0L; i--) { if((long) Math.floor(num /
         * (long) Math.pow(16, i)) == 10L) returnBuf.append("A"); else if((long)
         * Math.floor(num / (long) Math.pow(16, i)) == 11L)
         * returnBuf.append("B"); else if((long) Math.floor(num / (long)
         * Math.pow(16, i)) == 12L) returnBuf.append("C"); else if((long)
         * Math.floor(num / (long) Math.pow(16, i)) == 13L)
         * returnBuf.append("D"); else if((long) Math.floor(num / (long)
         * Math.pow(16, i)) == 14L) returnBuf.append("E"); else if((long)
         * Math.floor(num / (long) Math.pow(16, i)) == 15L)
         * returnBuf.append("F"); else returnBuf.append(Long.toString((long)
         * Math.floor(num / (long) Math.pow(16, i))));
         * 
         * num %= (long) Math.pow(16, i); }
         */

        return (num < 16L) ? "0" + Long.toString(num) : Long.toString(num, 16);
    }
}

Related

  1. long2hexString(long x)
  2. long2HexString(StringBuilder buf, long value)
  3. longToHex(long a)
  4. longToHex(long l)
  5. longToHex(long l, int length)
  6. longToHex(long val, char delim)
  7. longToHexBytes(final long v)
  8. longToHexBytes(long i)
  9. longToHexChars(long value, int length)