lizhanwei
2020-03-04 6121e0bde700d5595d70a03217f92e99929e54b6
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
package com.safeluck.aykj.decoder;
 
import com.safeluck.aykj.utils.BitState;
import com.safeluck.aykj.utils.BytesUtils;
 
/**
 * Created by zhouwei on 2016/12/1.
 */
public class BitStateCoder extends BaseDecoder<BitState> {
    Int16Coder int16Coder = new Int16Coder();
    Int32Coder int32Coder = new Int32Coder();
    @Override
    public BitState decode(String str) {
        if(str.length()==2)
        {
            return new BitState(Byte.valueOf(str,16));
        }
        if(str.length()==4)
        {
            return new BitState(Short.valueOf(str,16));
        }
        if(str.length()==8)
        {
            return new BitState(Integer.valueOf(str,16));
        }
        throw new RuntimeException("BitState长度不对,数据="+str);
    }
 
    @Override
    public String encode(BitState i) {
        if(i.total==8)
        {
            return BytesUtils.toHexString((byte)i.value);
        }
        if(i.total==16)
        {
            return int16Coder.encode((int)i.value);
        }
        if(i.total==32)
        {
            return int32Coder.encode((int)i.value);
        }
        throw new RuntimeException("BitState长度不对,数据="+i.total);
    }
}