Java Long to Hex longToHex(long val, char delim)

Here you can find the source of longToHex(long val, char delim)

Description

long To Hex

License

Open Source License

Declaration

public static String longToHex(long val, char delim) 

Method Source Code

//package com.java2s;
/**//  w ww.jav  a2  s .  co  m
 * Copyright (c) 2014 xio4
 * Universal bot for lineage-like games (Archeage, Lineage2 etc)
 *
 * This file is part of Unibot.
 *
 * Unibot 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 3 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String longToHex(long val, char delim) {
        byte[] bytes = new byte[8];
        for (int i = 7; i >= 0; i -= 1) {
            bytes[7 - i] = (byte) ((val >>> (i * 8)) & 0xFF);
        }
        return bytesToHex(bytes, delim);
    }

    /**
     * Convert bytes to hex string
     * 
     * @param bytes
     * @return hex string
     */

    public static String bytesToHex(byte[] bytes, int size, char delim) {
        char[] hexChars;
        if (bytes == null)
            return "";
        if (delim == 0) {
            hexChars = new char[size * 2];
            for (int j = 0; j < size; j++) {
                int v = bytes[j] & 0xFF;
                hexChars[j * 2] = hexArray[v >>> 4];
                hexChars[j * 2 + 1] = hexArray[v & 0x0F];
            }
        } else {
            hexChars = new char[size * 3];
            for (int j = 0; j < size; j++) {
                int v = bytes[j] & 0xFF;
                hexChars[j * 3] = hexArray[v >>> 4];
                hexChars[j * 3 + 1] = hexArray[v & 0x0F];
                hexChars[j * 3 + 2] = delim;
            }
        }
        return new String(hexChars);
    }

    public static String bytesToHex(byte[] bytes, char delim) {
        return bytesToHex(bytes, bytes.length, delim);

    }
}

Related

  1. long2HexString(StringBuilder buf, long value)
  2. longToHex(long a)
  3. longToHex(long l)
  4. longToHex(long l, int length)
  5. longToHex(long num)
  6. longToHexBytes(final long v)
  7. longToHexBytes(long i)
  8. longToHexChars(long value, int length)
  9. longToHexStr(long src, int len, int code)