常用数据格式转换下面展示一些 内联代码片
。
####会继续更新
//string字符串转byte[]
public static byte[] hexStringToBytes1(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
public static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
//BBC校验
public static String getBCC(byte[] data) {
String ret = "";
byte BCC[] = new byte[1];
for (int i = 0; i < data.length; i++) {
BCC[0] = (byte) (BCC[0] ^ data[i]);
}
String hex = Integer.toHexString(BCC[0] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
return ret;
}
public int hexCharToInt(char c) {
if (c >= '0' && c <= '9') return (c - '0');
if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
throw new RuntimeException("invalid hex char '" + c + "'");
}
public byte[] hexStringToBytes(String s) {
byte[] ret;
if (s == null) return null;
int sz = s.length();
ret = new byte[sz / 2];
for (int i = 0; i < sz; i += 2) {
ret[i / 2] = (byte) ((hexCharToInt(s.charAt(i)) << 4)
| hexCharToInt(s.charAt(i + 1)));
}
return ret;
}
//byte[] 转 16进制
public static String bytesToHexString(byte[] bArr) {
StringBuffer sb = new StringBuffer(bArr.length);
String sTmp;
for (int i = 0; i < bArr.length; i++) {
sTmp = Integer.toHexString(0xFF & bArr[i]);
if (sTmp.length() < 2)
sb.append(0);
sb.append(sTmp.toUpperCase() + " ");
}
return sb.toString();
}
/**
* 把16进制字符串转换成字节数组
*
* @param hex
* @return byte[]
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static int toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 获取BCC校验码
*
* @param data
* @param start 开始位置0
* @param end 字节数组长度
* @return
*/
public static String getBCC(byte[] data, int start, int end) {
String ret = "";
byte BCC[] = new byte[1];
for (int i = start; i < data.length; i++) {
if (i == end) {
break;
}
BCC[0] = (byte) (BCC[0] ^ data[i]);
}
String hex = Integer.toHexString(BCC[0] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
return ret;
}
//获取BCC校验码
public static String getBCC(char[] data) {
String ret = "";
byte BCC[] = new byte[1];
for (int i = 0; i < data.length; i++) {
BCC[0] = (byte) (BCC[0] ^ data[i]);
}
String hex = Integer.toHexString(BCC[0] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
return ret;
}
//String转字节
public byte[] getBytes(String[] str) {
StringBuilder b = new StringBuilder();
for (String s : str) {
b.append(s);
}
byte a[] = b.toString().getBytes();// b.toString.getBytes();
return a;
}
//异异或或校校验
private String getXORCheck(byte[] b) {
char x = 0;
for (int i = 0; i < b.length; i++)
x ^= b[i];
return String.format("%x", (int) x);
}
//ASCII码转换为16进制
public String convertStringToHex(String str) {
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
hex.append(Integer.toHexString((int) chars[i]));
}
return hex.toString();
}
//16进制换为ASCII码转
public String convertHexToString(String hex) {
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for (int i = 0; i < hex.length() - 1; i += 2) {
//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char) decimal);
temp.append(decimal);
}
return sb.toString();
}
//16转2
public static String hexString2binaryString(String hexString) {
if (hexString == null || hexString.length() % 2 != 0)
return null;
String bString = "", tmp;
for (int i = 0; i < hexString.length(); i++) {
tmp = "0000" + Integer.toBinaryString( Integer.parseInt( hexString.substring( i, i + 1 ), 16 ) );
bString += tmp.substring( tmp.length() - 4 );
}
return bString;
}
//取后面两位
public static double convert(float value) {
long l1 = Math.round( value * 100 ); //四舍五入
double ret = l1 / 100.0; //注意:使用 100.0 而不是 100
return ret;
}
/**
* 截取byte数组 不改变原数组
*
* @param b 原数组
* @param off 偏差值(索引)
* @param length 长度
* @return 截取后的数组
*/
public static byte[] subByte(byte[] b, int off, int length) {
byte[] b1 = new byte[length];
System.arraycopy( b, off, b1, 0, length );
return b1;
}
//转int
public static int shu(byte b) {
int i = Integer.parseInt( (String.format( "%02X", b )), 16 );
return i;
}
//byte[] 转 16进制
public static String bytesToHexString(byte[] bArr) {
StringBuffer sb = new StringBuffer( bArr.length );
String sTmp;
for (int i = 0; i < bArr.length; i++) {
sTmp = Integer.toHexString( 0xFF & bArr[i] );
if (sTmp.length() < 2)
sb.append( 0 );
sb.append( sTmp.toUpperCase() + " " );
}
return sb.toString();
}