Java Long to Hex longToHex(long a)

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

Description

long To Hex

License

Open Source License

Declaration

public static final String longToHex(long a) 

Method Source Code

//package com.java2s;
/**//w  w w  .jav a2s . c  o  m
  *         Commmon Internet File System Java API (JCIFS)
  *----------------------------------------------------------------
  *  Copyright (C) 1999  Norbert Hranitzky
  *
  *  This program 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.
  *
  *  This program 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 this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  *  The full copyright text: http://www.gnu.org/copyleft/gpl.html
  *
  *----------------------------------------------------------------
  *  Author: Norbert Hranitzky
  *  Email : norbert.hranitzky@mchp.siemens.de
  *  Web   : http://www.hranitzky.purespace.de
  */

public class Main {
    public static final String longToHex(long a) {
        byte[] b = new byte[8];
        longToBytes(a, b, 0);
        return bytesToHex(b);
    }

    public static final void longToBytes(long a, byte[] b, int bo) {
        b[bo] = (byte) (a >> 56);
        b[bo + 1] = (byte) (a >> 48);
        b[bo + 2] = (byte) (a >> 40);
        b[bo + 3] = (byte) (a >> 32);
        b[bo + 4] = (byte) (a >> 24);
        b[bo + 5] = (byte) (a >> 16);
        b[bo + 6] = (byte) (a >> 8);
        b[bo + 7] = (byte) (a);
    }

    public static final String bytesToHex(byte[] a) {

        return bytesToHex(a, a.length);
    }

    public static final String bytesToHex(byte[] a, int len) {
        StringBuffer s = new StringBuffer();

        for (int i = 0; i < len; ++i) {
            s.append(Character.forDigit((a[i] >> 4) & 0xf, 16));
            s.append(Character.forDigit(a[i] & 0xf, 16));
        }
        return s.toString();
    }
}

Related

  1. Long2HexString(long h)
  2. long2hexString(long x)
  3. long2HexString(StringBuilder buf, long value)
  4. longToHex(long l)
  5. longToHex(long l, int length)
  6. longToHex(long num)
  7. longToHex(long val, char delim)