蓝牙业务协议工具类
package com.rrioo.sateliteone4sf.bluetooth.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.rrioo.sateliteone4sf.util.DLog;
import com.rrioo.sateliteone4sf.util.Utils;
import android.text.TextUtils;
import android.util.Log;
/**
* @version :2015年1月28日 下午2:03:54
*
* 类说明 :数据类型转换
*/
public class DataSwitchUtils {
private static final String TAG = "DataSwitch";
//
/**
* 字节到字符转换
*
* @param b
* @return
*/
public static char getChar(byte[] b, int index) {
int s = 0;
if (b[index + 1] > 0)
s += b[index + 1];
else
s += 256 + b[index + 0];
s *= 256;
if (b[index + 0] > 0)
s += b[index + 1];
else
s += 256 + b[index + 0];
char ch = (char) s;
return ch;
}
/**
* 将一个2个字节数组转换为intt。
*
* @param b
* @return
*/
public static int bytesTwo2Int(byte[] b) {
int a1 = b[0] & 0xff;
int a2 = (b[1] & 0xff) * 256;
return (a1 + a2);
}
/**
* 将一个4个字节数组转换为intt。
*
* @param b
* @return
*/
public static int bytesFour2Int(byte[] b) {
int a1 = b[0] & 0xff;
int a2 = (b[1] & 0xff) * 256;
int a3 = (b[2] & 0xff) * 65536;
int a4 = (b[3] & 0xff) * 16777216;
return (a1 + a2 + a3 + a4);
}
public static String ten2sixteen(long ten) {
return Long.toHexString(ten);
}
public static byte[] long2Bytes(long num) {
byte[] byteNum = new byte[8];
for (int ix = 0; ix < 8; ++ix) {
int offset = 64 - (ix + 1) * 8;
byteNum[ix] = (byte) ((num >> offset) & 0xff);
}
return byteNum;
}
public static byte[] int2Bytes(int num) {
byte[] byteNum = new byte[4];
for (int ix = 0; ix < 4; ++ix) {
int offset = 32 - (ix + 1) * 8;
byteNum[ix] = (byte) ((num >> offset) & 0xff);
}
DLog.e("--------------byteNum[3] = " + byteNum[3]);
DLog.e("--------------byteNum[3] = " + byteNum[2]);
byte[] byte_result = new byte[] { byteNum[3], byteNum[2] };
return byte_result;
}
/**
* Convert hex string to byte[]
*
* @param hexString
* the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(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;
}
/**
* Convert char to byte
*
* @param c
* char
* @return byte
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* ascii to string
*
* @param value
* @return
*/
public static String asciiToString(String value) {
StringBuffer sbu = new StringBuffer();
String[] chars = value.split(",");
for (int i = 0; i < chars.length; i++) {
sbu.append((char) Integer.parseInt(chars[i]));
}
return sbu.toString();
}
/**
* string to ascii
*
* @param value
* @return
*/
public static String stringToAscii(String value) {
StringBuffer sbu = new StringBuffer();
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (i != chars.length - 1) {
sbu.append((int) chars[i]).append(",");
} else {
sbu.append((int) chars[i]);
}
}
return sbu.toString();
}
@SuppressWarnings({ "resource" })
public static byte[] getByte(File file) {
byte[] bytes = null;
if (file != null) {
try {
InputStream is = new FileInputStream(file);
int length = (int) file.length();
Log.d(TAG, "the file length is= " + length);
if (length > Integer.MAX_VALUE) {
System.out.println("this file is max");
return null;
}
bytes = new byte[length];
int offset = 0;
int numRead = 0;
while (offset == 0) {
offset += numRead;
}
if (offset != length) {
System.out.println("file length is error");
return null;
}
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bytes;
}
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
* 十六进制转换字符串
*
* @param String
* str Byte字符串(Byte之间无分隔符 如:[616C6B])
* @return String 对应的字符串
*/
public static String hexStr2Str(String hexStr) {
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
/**
* 十六进制转二进制
* @修改者
* @param a
* @return
*/
public static String HToB(String a) {
String b = Integer.toBinaryString(Integer.valueOf(toD(a, 16)));
return b;
}
/**
* 二进制转十六进制
* @修改者
* @param a
* @return
*/
public static String BToH(String a) {
/* 将二进制转为十进制再从十进制转为十六进制*/
String b = Integer.toHexString(Integer.valueOf(toD(a, 2)));
return b;
}
/**
* 任意进制数转为十进制数
* @修改者
* @param a
* @param b
* @return
*/
public static String toD(String a, int b) {
int r = 0;
for (int i = 0; i < a.length(); i++) {
r = (int) (r + formatting(a.substring(i, i + 1))
* Math.pow(b, a.length() - i - 1));
}
return String.valueOf(r);
}
/**
* 将十六进制中的字母转为对应的数字
* @修改者
* @param a
* @return
*/
public static int formatting(String a) {
int i = 0;
for (int u = 0; u < 10; u++) {
if (a.equals(String.valueOf(u))) {
i = u;
}
}
if (a.equals("a")) {
i = 10;
}
if (a.equals("b")) {
i = 11;
}
if (a.equals("c")) {
i = 12;
}
if (a.equals("d")) {
i = 13;
}
if (a.equals("e")) {
i = 14;
}
if (a.equals("f")) {
i = 15;
}
return i;
}
/**
* 将十进制中的数字转为十六进制对应的字母
* @修改者
* @param a
* @return
*/
public static String formattingH(int a) {
String i = String.valueOf(a);
switch (a) {
case 10:
i = "a";
break;
case 11:
i = "b";
break;
case 12:
i = "c";
break;
case 13:
i = "d";
break;
case 14:
i = "e";
break;
case 15:
i = "f";
break;
}
return i;
}
/**
* 添加字符串长度到purLen长度,添加内容为0;添加在原字符串前面
* @修改者
* @param str
* @param purLen
* @return
*/
public static String appendStrLen(String str, int purLen){
if(Utils.isNull(str) || str.length() >= purLen){
return str;
}
StringBuffer sb = new StringBuffer();
int subLen = purLen - str.length();
for (int i = 0; i < subLen; i++) {
sb.append("0");
}
sb.append(str);
return sb.toString();
}
public static int sixteenToTen(String sixteenStr){
int i = 0;
try {
i = Integer.parseInt(sixteenStr, 16);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
}