//byte数组转换成十六进制字符串
public static void BytePrintAsString(byte [] byteArray) {
for (int i = 0; i < byteArray.length; i++) {
String hex = Integer.toHexString(byteArray[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase());
}
System.out.println();
}
/**
* 16进制字符串数组转byte数组
*
* @param hex
* @return
*/
public static byte[] Hex2Byte(String[] hex) {
byte[] byteArray = new byte[hex.length];
for (int i = 0; i < hex.length; i++) {
//parseInt()方法用于将字符串参数作为有符号的n进制整数进行解析
byteArray[i] = (byte) Integer.parseInt(hex[i], 16);
}
return byteArray;
}
/**
* byte数组转成十六进制字符串
* @param bytes
* @return
*/
public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
/**
* double格式化,返回后面deg个小数,四舍五入
*
* @param data
* @param deg
* @return
*/
public static double doubleFormat(double data, int deg) {
double dy = new BigDecimal((double) data).setScale(deg, BigDecimal.ROUND_HALF_UP).doubleValue();
return dy;
}
/**
* float格式化,返回后面deg个小数,四舍五入
*
* @param data
* @param deg
* @return
*/
public static float floatFormat(float data, int deg) {
float dy = new BigDecimal((float) data).setScale(deg, BigDecimal.ROUND_HALF_UP).floatValue();
return dy;
}
//float转换成byte数组
public static byte[] floatTobyteArray(float v) {
ByteBuffer bb = ByteBuffer.allocate(4);
byte[] ret = new byte[4];
FloatBuffer fb = bb.asFloatBuffer();
fb.put(v);
bb.get(ret);
return ret;
}
//byte数组转换成int
public static int byteArrayToInt(byte[] v) {
ByteBuffer bb = ByteBuffer.wrap(v);
IntBuffer fb = bb.asIntBuffer();
return fb.get();
}
//byte数组转换成float
public static float byteArrayToFloat(byte[] v) {
ByteBuffer bb = ByteBuffer.wrap(v);
FloatBuffer fb = bb.asFloatBuffer();
return fb.get();
}
//byte数组转换成double
public static double byteArrayToDouble(byte[] v) {
ByteBuffer bb = ByteBuffer.wrap(v);
DoubleBuffer fb = bb.asDoubleBuffer();
return fb.get();
}
/**
* 将int转成byte数组
* int占4个字节
*
* @param v
* @return
*/
public static byte[] intToByteArray(int v) {
ByteBuffer bb = ByteBuffer.allocate(4);
byte[] ret = new byte[4];
IntBuffer fb = bb.asIntBuffer();
fb.put(v);
bb.get(ret);
return ret;
}
/**
* 将float转成byte数组
* float占4个字节
*
* @param v
* @return
*/
public static byte[] floatToByteArray(float v) {
ByteBuffer bb = ByteBuffer.allocate(4);
byte[] ret = new byte[4];
FloatBuffer fb = bb.asFloatBuffer();
fb.put(v);
bb.get(ret);
return ret;
}
/**
* 将double转成byte数组
* double占8个字节
*
* @param v
* @return
*/
public static byte[] doubleToByteArray(double v) {
ByteBuffer bb = ByteBuffer.allocate(8);
byte[] ret = new byte[8];
DoubleBuffer fb = bb.asDoubleBuffer();
fb.put(v);
bb.get(ret);
return ret;
}
/**
* byte[]转成float[]
* 大小端转换,其中每一个float的四位都要换位置,
* @param v
* @return
*/
public static float[] byteArrayToFloatArray(byte[] v) {
if (null == v || v.length%4 != 0){
return null;
}
float[] floats = new float[v.length/4];
for (int i=0;i<v.length;i=i+4){
byte[] b = new byte[4];
for (int j=0;j<4;j++){
b[j] = v[i+3-j];
}
floats[i/4] = byteArrayToFloat(b);
}
return floats;
}
/**
* 将测向打点数据转成byte数据返回,幅度为值double,角度值为int
* 这个返回是要返回给scpi使用
*
* @param oriDataList
* @return
*/
public static byte[] getOriDotByteData(List<float[]> oriDataList) {
if (null == oriDataList) {
return null;
}
int size = oriDataList.size();
byte[] returnByte = new byte[size * 12];//double 8字节,int 4字节
for (int i = 0; i < oriDataList.size(); i++) {
float[] data = oriDataList.get(i);
if (i >= size || data.length != 2) {
return null;
}
double amplitude = data[0];
int angele = (int) data[1];
byte[] a = doubleToByteArray(amplitude);
byte[] b = intToByteArray(angele);
returnByte[i * 12] = a[7];
returnByte[i * 12 + 1] = a[6];
returnByte[i * 12 + 2] = a[5];
returnByte[i * 12 + 3] = a[4];
returnByte[i * 12 + 4] = a[3];
returnByte[i * 12 + 5] = a[2];
returnByte[i * 12 + 6] = a[1];
returnByte[i * 12 + 7] = a[0];
returnByte[i * 12 + 8] = b[3];
returnByte[i * 12 + 9] = b[2];
returnByte[i * 12 + 10] = b[1];
returnByte[i * 12 + 11] = b[0];
}
return returnByte;
}
/**
* byte[]转成List<float[]>, 其中float[]长度为2,第一个是幅度值double,第二个是角度值int
* @param v byte存储的是 double int方式
* 8位double幅度值 4位int角度值
* @return
*/
public static List<float[]> byteArrayToOriList(byte[] v) {
if (null == v || v.length%12 != 0){
return null;
}
List<float[]> list = new ArrayList<>();
for (int i=0;i<v.length;i=i+12){
float[] floats = new float[2];
byte[] amp = new byte[8];
for (int j=0;j<8;j++){
amp[j] = v[i+7-j];
}
byte[] angle = new byte[4];
for (int j=0;j<4;j++){
angle[j] = v[i+11-j];
}
double dBm = byteArrayToDouble(amp);
floats[0] = (float) dBm;
floats[1] = byteArrayToInt(angle);
floats[1] = (90 - floats[1] + 360) % 360;
list.add(floats);
}
return list;
}
/**
* double直接转字符串是带E的,科学计数法,用此方法,可以转成不带科学计数法的字符串
* @param d
* @return
*/
public static String doubleToStr(double d){
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
String str = nf.format(d);
return str;
}
/**
* 将v变量的第n为置为0
* 比如 数字7 00000111 将其第二位变为0
* 数字1 00000001
*将1左移2-1位 00000010
* 取反 11111101
*与7与运算得到 00000101 返回5
* @param v
* @param n
* @return
*/
public static int setbit0(int v, int n) {
int t = ~(1 << (n - 1)) & v;
return t;
}
/**
* 将v变量的第n为置为1
* @param v
* @param n
* @return
*/
public static int setbit1(int v, int n) {
int t = (1 << (n - 1)) | v;
return t;
}
时间格式
yyyy-MM-dd HH:mm:ss.SSS年-月-日 时:分:秒.毫秒