import java.io.*;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字节转换工具类
*/
public class HexUtils {
//10进制转十六进制
// 低字节在前(低字节序)
public static byte[] toLH(int n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
//10进制转十六进制
//高字节在前(高字节序)
public static byte[] toHH(int n) {
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
//byte[]转String
public static String bytesToString(byte[] value){
StringBuilder stringBuilder=new StringBuilder(value.length*2);
for(byte b:value)
{
stringBuilder.append(String.format("%02X",new Integer(b & 0xFF)));
}
return stringBuilder.toString();
}
//打印byte[]
public static void printHexString( byte[] b) {
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase()+" , ");
}
System.out.println();
}
//byte[]转double
public static double byteArrayToDouble(byte[] bytes){
long ff = 0xFF;
long bitLayoutLongValue = 0;
for (int i = 0; i < bytes.length; i++) {
bitLayoutLongValue |= (bytes[i] & ff) << (bytes.length - i - 1) * 8;
}
return Double.longBitsToDouble(bitLayoutLongValue);
}
//byte[]转file
public static void byteArrayToFile(byte[] src, File dest) {
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(dest));
InputStream is = new ByteArrayInputStream(src);
byte[] flushBuffer = new byte[src.length];
int readLen = -1;
while ((readLen = is.read(flushBuffer)) != -1) {
os.write(flushBuffer, 0, readLen);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* byte数组转int类型
*/
public static int byteArrayToInt(byte[] bytes) {
int ff = 0xFF;
int value = 0;
for (int i = 0; i < bytes.length; i++) {
value |= (bytes[i] & ff) << (bytes.length - i - 1) * 8;
}
return value;
}
/**
* byte数组转float类型
*/
public static float byteArrayToFloat(byte[] b) {
return Float.intBitsToFloat(Integer.valueOf(bytesToString(b).trim(), 16));
}
/**
* String转byte数组
*/
public static byte[] toByteArray(String hexString) {
hexString = hexString.toLowerCase();
final byte[] byteArray = new byte[hexString.length() / 2];
int k = 0;
for (int i = 0; i < byteArray.length; i++) { //因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
byteArray[i] = (byte) (high << 4 | low);
k += 2;
}
return byteArray;
}
/**
* byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
*/
public static int bytesToInt(byte[] src, int offset) {
int value;
value = (int) ((src[offset] & 0xFF)
| ((src[offset+1] & 0xFF)<<8)
| ((src[offset+2] & 0xFF)<<16)
| ((src[offset+3] & 0xFF)<<24));
return value;
}
/**
* byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用
*/
public static int bytesToInt2(byte[] src, int offset) {
int value;
value = (int) ( ((src[offset] & 0xFF)<<24)
|((src[offset+1] & 0xFF)<<16)
|((src[offset+2] & 0xFF)<<8)
|(src[offset+3] & 0xFF));
return value;
}
}
HexUtils工具类,支持byte数组和Integer、float、double、String、File各种类型转换
亲测有效