java 中怎样将 bytes 转换为 long 类型

Python022

java 中怎样将 bytes 转换为 long 类型,第1张

方法有以下三种:

1、不借助其他任何已经有的类,直接进行转换。

2、借助java.nio.ByteBuffer实现,只要将byte[]转换为ByteBuffer就可以实现所有primitive类型的数据读取。

3、借助java.io.DataInputStream实现,只要将byte[]转换为DataInputStream就可以实现所有primitive类型的数据读取。

具体步骤如下:

1、/**

* 将字节数组转为long<br>

* 如果input为null,或offset指定的剩余数组长度不足8字节则抛出异常

* @param input

* @param offset 起始偏移量

* @param littleEndian 输入数组是否小端模式

* @return

*/

public static long longFrom8Bytes(byte[] input, int offset, boolean littleEndian){        long value=0       // 循环读取每个字节通过移位运算完成long的8个字节拼装

for(int  count=0count<8++count){            int shift=(littleEndian?count:(7-count))<<3

value |=((long)0xff<<shift) &((long)input[offset+count] <<shift)

}        return value

}

2、/**

* 利用 {@link java.nio.ByteBuffer}实现byte[]转long

* @param input

* @param offset

* @param littleEndian 输入数组是否小端模式

* @return

*/

public static long bytesToLong(byte[] input, int offset, boolean littleEndian) {

// 将byte[] 封装为 ByteBuffer

ByteBuffer buffer = ByteBuffer.wrap(input,offset,8)       if(littleEndian){            // ByteBuffer.order(ByteOrder) 方法指定字节序,即大小端模式(BIG_ENDIAN/LITTLE_ENDIAN)

// ByteBuffer 默认为大端(BIG_ENDIAN)模式

buffer.order(ByteOrder.LITTLE_ENDIAN)

}        return buffer.getLong()

}

3、public void test() throws IOException {

String input="net.gdface.facelog.dborm.person.FlPersonBeanBase"     

byte[] md5 = getMD5(input.getBytes())

System.out.printf("md5 [%s]\n",toHex(md5))   

// 三种方式运算结果对比验证

DataInputStream dataInput = new DataInputStream(new ByteArrayInputStream(md5))

long l1 = dataInput.readLong()   

long l2 = dataInput.readLong()

System.out.printf("l1=0x%x l2=0x%x,DataInputStream\n", l1,l2) 

long ln1 = bytesToLong(md5,0, false) 

long ln2 = bytesToLong(md5,8, false)

System.out.printf("ln1=0x%x ln2=0x%x,ByteBuffer\n", ln1,ln2) 

long ll1 = longFrom8Bytes(md5,0, false)

long ll2 = longFrom8Bytes(md5,8, false)

System.out.printf("ll1=0x%x ll2=0x%x\n", ll1,ll2)

}

}

方法有以下三种:

1、不借助其他任何已经有的类,直接进行转换。

2、借助java.nio.ByteBuffer实现,只要将byte[]转换为ByteBuffer就可以实现所有primitive类型的数据读取。

3、借助java.io.DataInputStream实现,只要将byte[]转换为DataInputStream就可以实现所有primitive类型的数据读取。

具体步骤如下:

1、/**

* 将字节数组转为long<br>

* 如果input为null,或offset指定的剩余数组长度不足8字节则抛出异常

* @param input

* @param offset 起始偏移量

* @param littleEndian 输入数组是否小端模式

* @return

*/

public static long longFrom8Bytes(byte[] input, int offset, boolean littleEndian){        long value=0       // 循环读取每个字节通过移位运算完成long的8个字节拼装

for(int  count=0count<8++count){            int shift=(littleEndian?count:(7-count))<<3

value |=((long)0xff<<shift) &((long)input[offset+count] <<shift)

}        return value

}

2、/**

* 利用 {@link java.nio.ByteBuffer}实现byte[]转long

* @param input

* @param offset

* @param littleEndian 输入数组是否小端模式

* @return

*/

public static long bytesToLong(byte[] input, int offset, boolean littleEndian) {

// 将byte[] 封装为 ByteBuffer

ByteBuffer buffer = ByteBuffer.wrap(input,offset,8)       if(littleEndian){            // ByteBuffer.order(ByteOrder) 方法指定字节序,即大小端模式(BIG_ENDIAN/LITTLE_ENDIAN)

// ByteBuffer 默认为大端(BIG_ENDIAN)模式

buffer.order(ByteOrder.LITTLE_ENDIAN)

}        return buffer.getLong()

}

3、public void test() throws IOException {

String input="net.gdface.facelog.dborm.person.FlPersonBeanBase"     

byte[] md5 = getMD5(input.getBytes())

System.out.printf("md5 [%s]\n",toHex(md5))   

// 三种方式运算结果对比验证

DataInputStream dataInput = new DataInputStream(new ByteArrayInputStream(md5))

long l1 = dataInput.readLong()   

long l2 = dataInput.readLong()

System.out.printf("l1=0x%x l2=0x%x,DataInputStream\n", l1,l2) 

long ln1 = bytesToLong(md5,0, false) 

long ln2 = bytesToLong(md5,8, false)

System.out.printf("ln1=0x%x ln2=0x%x,ByteBuffer\n", ln1,ln2) 

long ll1 = longFrom8Bytes(md5,0, false)

long ll2 = longFrom8Bytes(md5,8, false)

System.out.printf("ll1=0x%x ll2=0x%x\n", ll1,ll2)

}

}

byte即字节的意思,是java中的基本类型,用心申明字节型的变量。

通常在读取非文本文件时(如图片,声音,可执行文件)需要用字节数组来保存文件的内容。

在下载文件时,也是用byte数组作临时的缓冲器接收文件内容。所以说byte在文件操作时是必不可少的。不管是对文件写入还是读取都要用到。

扩展资料:

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

Java语言是一门随时代快速发展的计算机语言程序,其深刻展示了程序编写的精髓,加上其简明严谨的结构及简洁的语法编写为其将来的发展及维护提供了保障。

由于提供了网络应用的支持和多媒体的存取,会推动Internet和企业网络的Web的应用。

参考资料来源:百度百科-Java (计算机编程语言)