1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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;
//    }
 
 
}