panlili2024
2024-11-08 81d297dd33911dbfdc93dbc3fa9747e511b7ce8e
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.hdl.sdk.ttl.Utils.HDLUtlis;
 
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
public class HDLStringUtils {
 
    public static String byte2String(byte[] bytes) {
        int pos = bytes.length - 1;
        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, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static byte[] stringtoBytes(String str) {
        byte[] bytes = new byte[20];
        try {
            bytes = str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return bytes;
    }
 
    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();
    }
 
    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);
    }
 
 
 
//    //--------------------------20190626新增-----------------------------
//    // 判断奇数或偶数,位运算,最后一位是1则为奇数,为0是偶数
//    public static int isOdd(int num) {
//        return num & 1;
//    }
//
//    //-------------------------------------------------------
//    //Hex字符串转int
//    public static int HexToInt(String inHex) {
//        return Integer.parseInt(inHex, 16);
//    }
//
//    public static String IntToHex(int intHex){
//        return Integer.toHexString(intHex);
//    }
//
//    //-------------------------------------------------------
//    //Hex字符串转byte
//    public static byte HexToByte(String inHex) {
//        return (byte) Integer.parseInt(inHex, 16);
//    }
//
    //-------------------------------------------------------
    //1字节转2个Hex字符
    public static String Byte2Hex(Byte inByte) {
        return String.format("%02x", new Object[]{inByte}).toUpperCase();
    }
 
    //-------------------------------------------------------
//    //字节数组转转hex字符串
//    public static String ByteArrToHex(byte[] inBytArr) {
//        StringBuilder strBuilder = new StringBuilder();
//        for (byte valueOf : inBytArr) {
//            strBuilder.append(Byte2Hex(Byte.valueOf(valueOf)));
//            strBuilder.append(" ");
//        }
//        return strBuilder.toString();
//    }
 
    //-------------------------------------------------------
    //字节数组转转hex字符串,可选长度
    public static String ByteArrToHex(byte[] inBytArr, int offset, int byteCount) {
        StringBuilder strBuilder = new StringBuilder();
        int j = byteCount;
        for (int i = offset; i < j; i++) {
            strBuilder.append(Byte2Hex(Byte.valueOf(inBytArr[i])));
        }
        return strBuilder.toString();
    }
//
//    //-------------------------------------------------------
//    //转hex字符串转字节数组
//    public static byte[] HexToByteArr(String inHex) {
//        byte[] result;
//        int hexlen = inHex.length();
//        if (isOdd(hexlen) == 1) {
//            hexlen++;
//            result = new byte[(hexlen / 2)];
//            inHex = "0" + inHex;
//        } else {
//            result = new byte[(hexlen / 2)];
//        }
//        int j = 0;
//        for (int i = 0; i < hexlen; i += 2) {
//            result[j] = HexToByte(inHex.substring(i, i + 2));
//            j++;
//        }
//        return result;
//    }
//
//    /**
//     * 按照指定长度切割字符串
//     *
//     * @param inputString 需要切割的源字符串
//     * @param length      指定的长度
//     * @return
//     */
//    public static List<String> getDivLines(String inputString, int length) {
//        List<String> divList = new ArrayList<>();
//        int remainder = (inputString.length()) % length;
//        // 一共要分割成几段
//        int number = (int) Math.floor((inputString.length()) / length);
//        for (int index = 0; index < number; index++) {
//            String childStr = inputString.substring(index * length, (index + 1) * length);
//            divList.add(childStr);
//        }
//        if (remainder > 0) {
//            String cStr = inputString.substring(number * length, inputString.length());
//            divList.add(cStr);
//        }
//        return divList;
//    }
//
//    /**
//     * 计算长度,两个字节长度
//     *
//     * @param val value
//     * @return 结果
//     */
//    public static String twoByte(String val) {
//        if (val.length() > 4) {
//            val = val.substring(0, 4);
//        } else {
//            int l = 4 - val.length();
//            for (int i = 0; i < l; i++) {
//                val = "0" + val;
//            }
//        }
//        return val;
//    }
//
//    /**
//     * 校验和
//     *
//     * @param cmd 指令
//     * @return 结果
//     */
//    public static String sum(String cmd) {
//        List<String> cmdList = HDLStringUtils.getDivLines(cmd, 2);
//        int sumInt = 0;
//        for (String c : cmdList) {
//            sumInt += HDLStringUtils.HexToInt(c);
//        }
//        String sum = HDLStringUtils.IntToHex(sumInt);
//        sum = HDLStringUtils.twoByte(sum);
//        cmd += sum;
//        return cmd.toUpperCase();
//    }
 
 
 
 
//    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.I( "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.I( "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;
//    }
 
 
}