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 getDivLines(String inputString, int length) { // List 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 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; // } }