Java Long to Hex longToHexBytes(final long v)

Here you can find the source of longToHexBytes(final long v)

Description

Returns a string of spaced hex bytes in Big-Endian order.

License

Apache License

Parameter

Parameter Description
v the given long

Return

string of spaced hex bytes in Big-Endian order.

Declaration

public static String longToHexBytes(final long v) 

Method Source Code

//package com.java2s;
/*//from   w  ww .java  2  s.  c  om
 * Copyright 2015-16, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */

public class Main {
    /**
     * Returns a string of spaced hex bytes in Big-Endian order.
     * @param v the given long
     * @return string of spaced hex bytes in Big-Endian order.
     */
    public static String longToHexBytes(final long v) {
        final long mask = 0XFFL;
        final StringBuilder sb = new StringBuilder();
        for (int i = 8; i-- > 0;) {
            final String s = Long.toHexString((v >>> i * 8) & mask);
            sb.append(zeroPad(s, 2)).append(" ");
        }
        return sb.toString();
    }

    /**
     * Prepend the given string with zeros. If the given string is equal or greater than the given
     * field length, it will be returned without modification.
     * @param s the given string
     * @param fieldLength desired total field length including the given string
     * @return the given string prepended with zeros.
     */
    public static final String zeroPad(final String s, final int fieldLength) {
        return characterPad(s, fieldLength, '0', false);
    }

    /**
     * Prepend or postpend the given string with the given character to fill the given field length.
     * If the given string is equal or greater than the given field length, it will be returned
     * without modification.
     * @param s the given string
     * @param fieldLength the desired field length
     * @param padChar the desired pad character
     * @param postpend if true append the pacCharacters to the end of the string.
     * @return prepended or postpended given string with the given character to fill the given field
     * length.
     */
    public static final String characterPad(final String s, final int fieldLength, final char padChar,
            final boolean postpend) {
        final char[] chArr = s.toCharArray();
        final int sLen = chArr.length;
        if (sLen < fieldLength) {
            final char[] out = new char[fieldLength];
            final int blanks = fieldLength - sLen;

            if (postpend) {
                for (int i = 0; i < sLen; i++) {
                    out[i] = chArr[i];
                }
                for (int i = sLen; i < fieldLength; i++) {
                    out[i] = padChar;
                }
            } else { //prepend
                for (int i = 0; i < blanks; i++) {
                    out[i] = padChar;
                }
                for (int i = blanks; i < fieldLength; i++) {
                    out[i] = chArr[i - blanks];
                }
            }

            return String.valueOf(out);
        }
        return s;
    }
}

Related

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