Here you can find the source of longToBytesNoLeadZeroes(long val)
Parameter | Description |
---|---|
val | - long value to convert |
public static byte[] longToBytesNoLeadZeroes(long val)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; public class Main { public static final byte[] ZERO_BYTE_ARRAY = new byte[] { 0 }; /**// www .j av a2 s . c o m * Converts a long value into a byte array. * * @param val - long value to convert * @return decimal value with leading byte that are zeroes striped */ public static byte[] longToBytesNoLeadZeroes(long val) { // todo: improve performance by while strip numbers until (long >> 8 == 0) byte[] data = ByteBuffer.allocate(8).putLong(val).array(); return stripLeadingZeroes(data); } public static byte[] stripLeadingZeroes(byte[] data) { if (data == null) return null; final int firstNonZero = firstNonZeroByte(data); switch (firstNonZero) { case -1: return ZERO_BYTE_ARRAY; case 0: return data; default: byte[] result = new byte[data.length - firstNonZero]; System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero); return result; } } public static int firstNonZeroByte(byte[] data) { for (int i = 0; i < data.length; ++i) { if (data[i] != 0) { return i; } } return -1; } }