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 { 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); } }