package com.hdl.sdk.hdl_core.Util.TransformUtil;
|
|
import java.io.UnsupportedEncodingException;
|
|
/**
|
* Created by Tommy on 2017/7/21.
|
*/
|
|
public class StringUtil {
|
public static String byte2String(byte[] bytes) {
|
// String remarkStr = "备注数据:";
|
// for(int k = 0;k < bytes.length;k++){
|
// remarkStr +=(bytes[k]& 0xFF)+" ";
|
// }
|
// HDLLog.info(remarkStr);
|
|
int pos = bytes.length - 1;
|
// for(int i=0;i<bytes.length;i++){
|
//
|
// if(i!=(bytes.length-1) && (bytes[i]&0xFF)==0 && (bytes[i+1]&0xFF)==0){
|
// pos = i-1;
|
// break;
|
// }
|
// }
|
for (int i = 0; i < bytes.length; i++) {
|
|
if (i != (bytes.length - 1) && (bytes[i] & 0xFF) == 0) {
|
pos = i - 1;
|
break;
|
}
|
}
|
byte[] remarkBytes = new byte[pos + 1];
|
System.arraycopy(bytes, 0, remarkBytes, 0, remarkBytes.length);
|
|
String result = "";
|
|
try {
|
result = new String(remarkBytes, "GB2312");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
public static byte[] stringtoBytes(String str) {
|
byte[] bytes = new byte[20];
|
try {
|
bytes = str.getBytes("GB2312");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
return bytes;
|
}
|
|
public static String stringToAscii(String value) {
|
// stringToAscii("S1PLAYSTOP");
|
// 83,49,80,76,65,89,83,84,79,80
|
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();
|
}
|
|
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();
|
}
|
|
public static String byteToBit(byte b) {
|
return ""
|
+ (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
|
+ (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
|
+ (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
|
+ (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
|
}
|
|
|
public static float byteToFloat(byte b1, byte b2, byte b3, byte b4){
|
byte[] mByte = new byte[4];
|
mByte[0] = b1;
|
mByte[1] = b2;
|
mByte[2] = b3;
|
mByte[3] = b4;
|
return byteArrayToFloat(mByte);
|
}
|
|
// 从byte数组的index处的连续4个字节获得一个int
|
public static int getInt(byte[] arr) {
|
return (0xff000000 & (arr[0] << 24)) |
|
(0x00ff0000 & (arr[1] << 16)) |
|
(0x0000ff00 & (arr[2] << 8)) |
|
(0x000000ff & arr[3]);
|
}
|
|
// float转换为byte[4]数组
|
public static byte[] getByteArray(float f) {
|
int intbits = Float.floatToIntBits(f);//将float里面的二进制串解释为int整数
|
return getByteArray(intbits);
|
}
|
|
// 从byte数组的index处的连续4个字节获得一个float
|
public static float byteArrayToFloat(byte[] arr) {
|
try {
|
return Float.intBitsToFloat(getInt(arr));
|
} catch (Exception e) {
|
e.printStackTrace();
|
return 0;
|
}
|
}
|
|
// private byte[] formatStyleHDLResponse(String responsePart1, String responsePart2, int maxLen) throws UnsupportedEncodingException {
|
// String responsePart3 = HDL_END_STR;
|
// byte tmp;
|
// byte[] bytePart1 = stringtoBytes(responsePart1);
|
// byte[] bytePart2Tmp;
|
// byte[] bytePart2;
|
// byte[] bytePart3;
|
//
|
//
|
// if(TextUtils.isEmpty(responsePart2)){
|
// responsePart2 = " ";
|
// }
|
//
|
// bytePart2Tmp = responsePart2.getBytes("Unicode");
|
// bytePart2 = new byte[bytePart2Tmp.length - 2];
|
// bytePart3 = stringtoBytes(responsePart3);
|
//
|
// HDLLog.info( "formatStyle1HDLResponse responsePart1 " + responsePart1 + " responsePart2 " + responsePart2 + " responsePart3 " + responsePart3);
|
//
|
// System.arraycopy(bytePart2Tmp, 2, bytePart2, 0, bytePart2Tmp.length-2);
|
// for(int i=0; i< bytePart2.length; i += 2){
|
// tmp = bytePart2[i+1];
|
// bytePart2[i+1] = bytePart2[i];
|
// bytePart2[i] = tmp;
|
// }
|
//
|
// int copyPart2Len = bytePart2.length;
|
// int extUnicodeLen = 2;
|
// byte [] byteExtUnicode = new byte[extUnicodeLen];
|
// byteExtUnicode[0] = 0x0;
|
// byteExtUnicode[1] = 0x3;
|
//
|
// if((maxLen) < bytePart2.length){
|
// copyPart2Len = maxLen;
|
// if(copyPart2Len % 2 != 0){
|
// copyPart2Len = copyPart2Len/2 * 2;
|
// }
|
// HDLLog.info( "formatStyle1HDLResponse copyPart2Len=" + copyPart2Len + ", maxLen=" + maxLen);
|
// }
|
//
|
// byte [] byteResponse = new byte[bytePart1.length + copyPart2Len + extUnicodeLen + bytePart3.length];
|
//
|
//
|
// System.arraycopy(bytePart1, 0, byteResponse, 0, bytePart1.length);
|
// System.arraycopy(bytePart2, 0, byteResponse, bytePart1.length, copyPart2Len);
|
// System.arraycopy(byteExtUnicode, 0, byteResponse, bytePart1.length + copyPart2Len, extUnicodeLen);
|
// System.arraycopy(bytePart3, 0, byteResponse, bytePart1.length + copyPart2Len + extUnicodeLen, bytePart3.length);
|
//
|
// //Utils.printBtyes(bytePart1);
|
// //Utils.printBtyes(bytePart2);
|
// //Utils.printBtyes(bytePart3);
|
// //Utils.printBtyes(byteResponse);
|
// return byteResponse;
|
// }
|
|
// public void sendHDLResponseDISPLINE4() throws UnsupportedEncodingException {
|
// //String response = "#S" + 1 + "DISPLINE4,\u0002" + songName + "\u0003" + HDL_END_STR;
|
// JDMusicStatus jdMusicStatus = BMClient.getInstance().getJDMusicStatus();
|
// String songName = jdMusicStatus.getSong() != null ? jdMusicStatus.getSong().getName(): "No Song";
|
//
|
// if(mCurHDLDeviceSource == HDL_DEVICE_SRC_BLUETOOTH || mCurHDLDeviceSource == HDL_DEVICE_SRC_LINEIN) {
|
// songName = " ";
|
// }
|
//
|
// String unicodeStr = songName;
|
// String responsePart1 = "#S" + mCurHDLDeviceSource + "DISPLINE4,\u0002";
|
// String responsePart2 = unicodeStr;
|
//
|
// byte[] byteResponse = formatStyleHDLResponse(responsePart1, responsePart2, HDL_MAX_DISPLINE_2_4_BYTE);
|
// sendResponse(byteResponse);
|
// return;
|
// }
|
|
|
}
|