public class Main { public static void main(String[] args) { long testValue = 1234567890123456789L; byte[] byteArray = LongToByteArrayConverter.longToBytes(testValue); // 打印转换后的字节数组 for (byte b : byteArray) { System.out.printf("%02X ", b); } } } 在这个测试函数中,我们定...
Java provide ByteBuffer class to do the same.to convert any byte array, first we need to allocate 8 bytes using ByteBuffer’s static method allocate, then put byteArray using put method and flip bytebuffer. by calling getLong() method we can get long value of that byte array. private long...
**/publicstaticbyte[] intToByteArray(ints) {byte[] targets =newbyte[2];for(inti = 0; i < 4; i++) {intoffset = (targets.length - 1 - i) * 8; targets[i]= (byte) ((s >>> offset) & 0xff); }returntargets; }/*** long to byte[] * *@params * long *@returnbyte[] ...
public byte[] longToByteArray(long x, int index) { byte[] bb = new byte[8]; bb[index + 7] = (byte) (x >> 56); bb[index + 6] = (byte) (x >> 48); bb[index + 5] = (byte) (x >> 40); bb[index + 4] = (byte) (x >> 32); bb[index + 3] = (byte) (x ...
length - i - 1) * 8; } return value; } /** * long转byte[] * @param value * @return byte[] */ public static byte[] longToByteArray(long value){ byte[] bytes = new byte[8]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) (value >> (bytes.length ...
result[2] = (byte) ((i >> 8) & 0xFF); result[3] = (byte) (i & 0xFF); return result; } /** * 转换long型为byte数组 * * @param bb * @param x * @param index */ public byte[] longToByteArray(long x, int index) { ...
return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL; } /** *将16位的short转换成byte数组 * * @param s * short * @return byte[] 长度为2 * */ public static byte[] shortToByteArray(short s) { ...
long转成byte[]其实和 int转byte[]的逻辑一样,只不过int是四个字节,long是八个字节。/** * long转byte[8],低字节在前 */ public static byte[] longToByteArrayByLow(long n) { byte[] bytes = new byte[8]; bytes[0] = (byte) (n & 0xff); bytes[1] = (byte) (n >>> 8 & 0xff)...
longvalue=123456789L;byte[]array=newbyte[Long.BYTES];for(inti=0;i<Long.BYTES;i++){array[i]=(byte)((value>>(i*8))&0xFF);} 1. 2. 3. 4. 5. 代码示例 下面是一个简单的示例,演示了如何将long类型数据转换为字节数组: publicclassLongToArrayExample{publicstaticvoidmain(String[]args){long...
| (((long) array[offset + 6] & 0xff) << 8) | (((long) array[offset + 7] & 0xff) << 0)); } public static byte[] intToBytes(int n) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); ...