fctom1215
2020-03-08 9f10b837804f147eb7a2722147c7917e0523d27c
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
package com.safeluck.aykj.decoder;
 
import com.safeluck.aykj.utils.BytesUtils;
 
/**
 * Created by zhouwei on 2016/12/1.
 */
public class Int16LECoder extends BaseDecoder<Integer> {
 
    public static Int16LECoder instance = new Int16LECoder();
 
    @Override
    public Integer decode(String str) {
 
        byte[] src = BytesUtils.hexStringToBytes(str);
        int offset = 0;
        int value;
        value =  (src[offset] & 0xFF)
                | ((src[offset + 1] & 0xFF) << 8);
//                | ((src[offset + 2] & 0xFF) << 16)
//                | ((src[offset + 3] & 0xFF) << 24));
        return value;
//        return Integer.valueOf(Short.reverseBytes(Int16Coder.instance.decode(str).shortValue())&0xFF);
//        if(str==null||"".equals(str))
//            return 0;
//
//        return Integer.valueOf(str, 16);
    }
 
    @Override
    public String encode(Integer i) {
        if(i==null)
            i=0;
        int value = i.intValue();
        byte[] src = new byte[2];
//        src[3] = (byte) ((value >> 24) & 0xFF);
//        src[2] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[0] = (byte) (value & 0xFF);
        return BytesUtils.bytesToHexString(src);
    }
}