Java Long to Hex longToHexBytes(long i)

Here you can find the source of longToHexBytes(long i)

Description

long To Hex Bytes

License

Apache License

Declaration

public static byte[] longToHexBytes(long i) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static byte[] longToHexBytes(long i) {
        byte[] bytes = new byte[16];
        oneByteToHexBytes((byte) ((i >>> 56) & 0xff), bytes, 0);
        oneByteToHexBytes((byte) ((i >>> 48) & 0xff), bytes, 2);
        oneByteToHexBytes((byte) ((i >>> 40) & 0xff), bytes, 4);
        oneByteToHexBytes((byte) ((i >>> 32) & 0xff), bytes, 6);
        oneByteToHexBytes((byte) ((i >>> 24) & 0xff), bytes, 8);
        oneByteToHexBytes((byte) ((i >>> 16) & 0xff), bytes, 10);
        oneByteToHexBytes((byte) ((i >>> 8) & 0xff), bytes, 12);
        oneByteToHexBytes((byte) (i & 0xff), bytes, 14);
        return bytes;
    }//from  w w  w  .  j  a  v  a2s  .c o m

    public static byte[] longToHexBytes(long i, byte[] bytes) {
        oneByteToHexBytes((byte) ((i >>> 56) & 0xff), bytes, 0);
        oneByteToHexBytes((byte) ((i >>> 48) & 0xff), bytes, 2);
        oneByteToHexBytes((byte) ((i >>> 40) & 0xff), bytes, 4);
        oneByteToHexBytes((byte) ((i >>> 32) & 0xff), bytes, 6);
        oneByteToHexBytes((byte) ((i >>> 24) & 0xff), bytes, 8);
        oneByteToHexBytes((byte) ((i >>> 16) & 0xff), bytes, 10);
        oneByteToHexBytes((byte) ((i >>> 8) & 0xff), bytes, 12);
        oneByteToHexBytes((byte) (i & 0xff), bytes, 14);
        return bytes;
    }

    public static byte[] longToHexBytes(long i, byte[] bytes, int startIdx) {
        oneByteToHexBytes((byte) ((i >>> 56) & 0xff), bytes, startIdx);
        oneByteToHexBytes((byte) ((i >>> 48) & 0xff), bytes, startIdx + 2);
        oneByteToHexBytes((byte) ((i >>> 40) & 0xff), bytes, startIdx + 4);
        oneByteToHexBytes((byte) ((i >>> 32) & 0xff), bytes, startIdx + 6);
        oneByteToHexBytes((byte) ((i >>> 24) & 0xff), bytes, startIdx + 8);
        oneByteToHexBytes((byte) ((i >>> 16) & 0xff), bytes, startIdx + 10);
        oneByteToHexBytes((byte) ((i >>> 8) & 0xff), bytes, startIdx + 12);
        oneByteToHexBytes((byte) (i & 0xff), bytes, startIdx + 14);
        return bytes;
    }

    private static byte[] oneByteToHexBytes(byte b, byte[] hexBytes, int startIdx) {
        byte high = (byte) ((b & 0xf0) >>> 4);
        if (high <= 9)
            high += '0';
        else
            high += ('A' - 10);
        hexBytes[startIdx] = high;

        byte low = (byte) (b & 0x0f);
        if (low <= 9)
            low += '0';
        else
            low += ('A' - 10);
        hexBytes[startIdx + 1] = low;

        return hexBytes;
    }
}

Related

  1. longToHex(long l)
  2. longToHex(long l, int length)
  3. longToHex(long num)
  4. longToHex(long val, char delim)
  5. longToHexBytes(final long v)
  6. longToHexChars(long value, int length)
  7. longToHexStr(long src, int len, int code)
  8. LongToHexString(final long value)
  9. longToHexString(long n, int digits)