fctom1215
2020-03-10 10c6d47260b4fb34262ec7a703a06fffa6a90ad0
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
282
283
284
285
package com.anyun.im_lib.util;
 
import android.text.TextUtils;
import android.util.Log;
 
import com.safeluck.aykj.utils.BytesUtils;
 
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.SimpleDateFormat;
 
/**
 * MyApplication2
 * Created by lzw on 2019/12/13. 12:01:18
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class ByteUtil {
 
private static final String TAG = "ByteUtil";
    /**
     * @功能: BCD码转为10进制串(阿拉伯数据)
     * @参数: BCD码
     * @结果: 10进制串
     */
    public static String bcd2Str(byte[] bytes) {
        StringBuffer temp = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
            temp.append((byte) (bytes[i] & 0x0f));
        }
        return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
                .toString().substring(1) : temp.toString();
    }
 
    /**
     * @功能: 10进制串转为BCD码
     * @参数: 10进制串
     * @结果: BCD码
     */
    public static byte[] str2Bcd(String asc) {
        int len = asc.length();
        int mod = len % 2;
        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }
        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }
        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;
        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }
            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }
            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
//        Log.i(TAG, "str2Bcd: "+byte2HexStr(bbt));
        return bbt;
    }
 
    public static String getString(byte[] bytes, String charsetName) {
        return new String(bytes, Charset.forName(charsetName));
    }
 
    public static String getString(byte[] bytes) {
        return getString(bytes, "GBK");
    }
 
    /**
     * bytes转换成十六进制字符串
     * @param byte[] b byte数组
     * @return String 每个Byte值之间空格分隔
     */
    public static String byte2HexStr(byte[] b)
    {
        String stmp="";
        StringBuilder sb = new StringBuilder("");
        for (int n=0;n<b.length;n++)
        {
            stmp = Integer.toHexString(b[n] & 0xFF);
            sb.append((stmp.length()==1)? "0"+stmp : stmp);
            sb.append(" ");
        }
        return sb.toString().toUpperCase().trim();
    }
 
    /**
     * bytes字符串转换为Byte值
     * @param String src Byte字符串,每个Byte之间没有分隔符
     * @return byte[]
     */
    public static byte[] hexStr2Bytes(String src)
    {
        src = src.replace(" ","");
        int m=0,n=0;
        int l=src.length()/2;
        System.out.println(l);
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++)
        {
            m=i*2+1;
            n=m+1;
            ret[i] = Byte.decode("0x" + src.substring(i*2, m) + src.substring(m,n));
        }
        return ret;
    }
 
    /**
     * 16进制的字符串表示转成字节数组
     *
     * @param hexString 16进制格式的字符串
     * @return 转换后的字节数组
     **/
    public static byte[] toByteArray(String hexString) {
        if (TextUtils.isEmpty(hexString))
            throw new IllegalArgumentException("this hexString must not be empty");
 
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }
 
 
    public static int getInt(byte i) {
        int low = i&0x0f;
        int hight= (i>>4)&0x0f;
        return low+hight*16;
    }
 
    public static int getBCDInt(byte i) {
        int low = i&0x0f;
        int hight= (i>>4)&0x0f;
        return low+hight*10;
    }
 
 
 
 
    /**
     * ByteBuffer 转换 String
     * @param buffer
     * @return
     */
    public static String getString(ByteBuffer buffer)
    {
        Charset charset = null;
        CharsetDecoder decoder = null;
        CharBuffer charBuffer = null;
        try
        {
            charset = Charset.forName("UTF-8");
            decoder = charset.newDecoder();
             charBuffer = decoder.decode(buffer);//用这个的话,只能输出来一次结果,第二次显示为空
//            charBuffer = decoder.decode(buffer.asReadOnlyBuffer());
            return charBuffer.toString();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return "";
        }
    }
 
    /**
     * short转为字节数组 大端
     * @param data
     * @return 包含2个字节的字节数组
     */
    public static byte[] shortGetBytes(short data) {
        byte[] bytes = new byte[2];
        bytes[1] = (byte) (data & 0xff);
        bytes[0] = (byte) ((data & 0xff00) >> 8);
        return bytes;
    }
 
    /**
     * 大端 int 转成 4个字节的byte数组
     * @param data
     * @return
     */
    public static byte[] intGetBytes(int data) {
        byte[] bytes = new byte[4];
        bytes[3] = (byte) ((data & 0xff));
        bytes[2] = (byte) ((data & 0xff00) >> 8);
        bytes[1] = (byte) ((data & 0xff0000) >> 16);
        bytes[0] = (byte) ((data & 0xff000000) >> 24);
        return bytes;
    }
 
    public static  byte[] shortGetByte(short data){
        byte[] bytes = new byte[1];
        bytes[0] = (byte)(data & 0xff);
        return bytes;
    }
 
    public static void main(String[] args){
        System.out.println(byte2HexStr(shortGetBytes((short) 65535)));
        short b = (short) 32768;
        b++;
        System.out.println(byte2HexStr(shortGetBytes((short) b)));
 
 
        String model = "123";
        byte[] messageBody = new byte[30];
        byte[] modelBytes = new byte[20];
        modelBytes =  model.getBytes();
        System.arraycopy(shortGetBytes((short)323),0,messageBody,0,2);
        System.arraycopy(modelBytes,0,messageBody,2,modelBytes.length);
        System.arraycopy(shortGetBytes((short)3),0,messageBody,22,2);
        System.out.println(byte2HexStr(messageBody));
 
 
        byte[] bytes =new byte[]{(byte) 0x80};
        short aShort= getShort(bytes);
        System.out.println(aShort);
 
 
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddhhmmss");
        String time = sdf.format(System.currentTimeMillis());
        byte[] timeStampBCD = ByteUtil.str2Bcd(time);
 
//        String hex = "B7 A2 B6 AF BB FA C6 F4 B6 AF BA F3 A3 AC B2 BB BC B0 CA B1 CB C9 BF AA C6 F4 B6 AF BF AA B9 D8";
//        String string = new String(BytesUtils.hexStringToBytes(hex.replace(" ","")));
//        System.out.println(string);
    }
 
    public static byte[] subArray(byte[] srcBytes, int begin, int length) {
        byte[] bytes = new byte[length];
        System.arraycopy(srcBytes,begin,bytes,0,length);
        Log.i(TAG, "subArray: "+byte2HexStr(bytes));
        return bytes;
    }
 
    public static short getShort(byte[] bytes) {
        short s =0;
        if (bytes.length==1){
            s|=(bytes[0]&0xff);
            return s;
        }
        s =(short) ((0xff & bytes[1]) | (0xff00 & (bytes[0] << 8)));
        Log.i(TAG, "getShort: "+s);
        return s ;
    }
 
    /**
     * 判断1一个int变量 某位bit的值
     *
     * @param num
     * @param pos =0  是第一为  1.。 2.。
     * @return
     */
    public static int getBitStatus(int num,int pos){
        if ((num&(1<<pos))!=0){
            return 1;
        }else
            return 0;
    }
}