| | |
| | | package com.ard.utils.other; |
| | | |
| | | import javax.xml.bind.DatatypeConverter; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.nio.ByteBuffer; |
| | |
| | | * @Version: 1.0 |
| | | **/ |
| | | public class ByteUtils { |
| | | |
| | | /** |
| | | * 打印十六进制二进制 |
| | | */ |
| | | public static String printHexBinary(byte[] bytes) { |
| | | return DatatypeConverter.printHexBinary(bytes); |
| | | } |
| | | |
| | | /** |
| | | * byte数组转中文字符串 |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * double转byte数组 |
| | | */ |
| | | public static byte[] doubleToBytes(double d) { |
| | | long value = Double.doubleToRawLongBits(d); |
| | | byte[] byteRet = new byte[8]; |
| | | for (int i = 0; i < 8; i++) { |
| | | byteRet[i] = (byte) ((value >> 8 * i) & 0xff); |
| | | } |
| | | return byteRet; |
| | | } |
| | | /** |
| | | * float转byte数组 |
| | | */ |
| | | public static byte[] floatToBytes(float f) { |
| | | // 创建一个 ByteBuffer,分配足够的空间来存储一个 float 值 |
| | | ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); |
| | | // 将 float 值写入 ByteBuffer |
| | | buffer.putFloat(f); |
| | | // 获取字节数组 |
| | | return buffer.array(); |
| | | } |
| | | /** |
| | | * byte数组拼接 |
| | | */ |
| | | public static byte[] appendArrays(byte[]... arrays) { |