Java Utililty Methods Long to

List of utility methods to do Long to

Description

The list of methods to do Long to are organized into topic(s).

Method

byte[]longToNetworkBytes(long value)
long To Network Bytes
return new byte[] { (byte) (value >>> 56), (byte) (value >>> 48), (byte) (value >>> 40),
        (byte) (value >>> 32), (byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8),
        (byte) value, };
char[]longToNumberWithDecimalPlaces(long longValue, int decimalPlaces)
converts a long into representation like double.
If longValue 12345 and decimalPlaces = 2 output is 123.45
if long value = 12 and decimalPlaces =3 output is 0.012
String value = Long.toString(longValue);
char[] charVal;
int pointPos = value.length() - decimalPlaces;
int copyStart = 0;
if (value.charAt(0) == '-') {
    copyStart = 1;
if (pointPos < 1 || (pointPos < 2 && copyStart == 1)) {
...
StringLongToOctString(final long value)
Long To Oct String
final String string = Long.toString(value, 8).toUpperCase();
if (string.charAt(0) == '-') {
    return "-0" + string.substring(1, string.length()) + "L";
} else {
    return "0" + string + "L";
ObjectlongToOther(Object val, Class dest)
long To Other
if (val instanceof Long) {
    Long l = (Long) val;
    if (dest.equals(Integer.class) || dest.equals(int.class)) {
        return l.intValue();
    } else if (dest.equals(Float.class) || dest.equals(float.class)) {
        return l.floatValue();
    } else if (dest.equals(Double.class) || dest.equals(double.class)) {
        return l.doubleValue();
...
intlongToPrefixCoded(final long val, final int shift, final char[] buffer)
Expert: Returns prefix coded bits after reducing the precision by shift bits.
if (shift > 63 || shift < 0)
    throw new IllegalArgumentException("Illegal shift value, must be 0..63");
int nChars = (63 - shift) / 7 + 1, len = nChars + 1;
buffer[0] = (char) (SHIFT_START_LONG + shift);
long sortableBits = val ^ 0x8000000000000000L;
sortableBits >>>= shift;
while (nChars >= 1) {
    buffer[nChars--] = (char) (sortableBits & 0x7f);
...
byte[]longToRegisters(long v)
Converts a long value to a byte[8].
byte[] registers = new byte[8];
registers[0] = (byte) (0xff & (v >> 56));
registers[1] = (byte) (0xff & (v >> 48));
registers[2] = (byte) (0xff & (v >> 40));
registers[3] = (byte) (0xff & (v >> 32));
registers[4] = (byte) (0xff & (v >> 24));
registers[5] = (byte) (0xff & (v >> 16));
registers[6] = (byte) (0xff & (v >> 8));
...
StringlongToRemoteAddrTo(long remoteAddr)
long To Remote Addr To
StringBuilder sb = new StringBuilder();
for (int i = 24; i >= 0; i -= 8) {
    long l = (remoteAddr >> i) & 0xff;
    if (sb.length() > 0) {
        sb.append(".");
    sb.append(l);
return sb.toString();
voidlongToSortableBytes(long value, byte[] result, int offset)
Encodes an long value such that unsigned byte order comparison is consistent with Long#compare(long,long)
value ^= 0x8000000000000000L;
result[offset] = (byte) (value >> 56);
result[offset + 1] = (byte) (value >> 48);
result[offset + 2] = (byte) (value >> 40);
result[offset + 3] = (byte) (value >> 32);
result[offset + 4] = (byte) (value >> 24);
result[offset + 5] = (byte) (value >> 16);
result[offset + 6] = (byte) (value >> 8);
...
StringlongToStringAddress(long addr)
long To String Address
if ((addr < 0) || (addr > MAX_ADDRESS))
    throw new IllegalArgumentException("invalid IP address: " + addr);
StringBuffer rc = new StringBuffer(16);
for (int i = 0; i < 4; i++) { 
    rc.insert(0, addr & 0xFFL);
    rc.insert(0, '.');
    addr >>= 8;
rc.delete(0, 1);
return rc.toString();
StringlongToStringWithZeroFill(long longValue, int width)
Convert long to string with left zero fill.
String s = new Long(longValue).toString();
if (s.length() < width)
    s = dupl('0', width - s.length()) + s;
return s;