java 解析8583 报文

首先 抽象一个类 表示每个域的数据

public class Area {
    private int areaNo;//域序号
    private String areaName;//域名称
    private int leng;//域长度(定长)
    private AreaDataType dataType;//域数据类型
    private AreaLenType lenType;//域长度类型
    private boolean isDynamicLen;//是否是变长
    private String data;//数据
    private int dynamicLen;//变长长度
    private int dynamicLen_2_3;//(变长长度是2/3字节)


    public Area() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Area(int areaNo, String areaName, int leng, AreaDataType dataType, AreaLenType lenType,
            boolean isDynamicLen,int dynamicLen_2_3) {
        super();
        this.areaNo = areaNo;
        this.areaName = areaName;
        this.leng = leng;
        this.dataType = dataType;
        this.lenType = lenType;
        this.isDynamicLen = isDynamicLen;
        this.dynamicLen_2_3 = dynamicLen_2_3;
    }

    public int getDynamicLen_2_3() {
        return dynamicLen_2_3;
    }

    public void setDynamicLen_2_3(int dynamicLen_2_3) {
        this.dynamicLen_2_3 = dynamicLen_2_3;
    }

    public int getDynamicLen() {
        return dynamicLen;
    }

    public void setDynamicLen(int dynamicLen) {
        this.dynamicLen = dynamicLen;
    }

    public boolean isDynamicLen() {
        return isDynamicLen;
    }

    public void setDynamicLen(boolean isDynamicLen) {
        this.isDynamicLen = isDynamicLen;
    }

    public int getAreaNo() {
        return areaNo;
    }
    public void setAreaNo(int areaNo) {
        this.areaNo = areaNo;
    }
    public String getAreaName() {
        return areaName;
    }
    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }
    public int getLeng() {
        return leng;
    }
    public void setLeng(int leng) {
        this.leng = leng;
    }
    public AreaDataType getDataType() {
        return dataType;
    }
    public void setDataType(AreaDataType dataType) {
        this.dataType = dataType;
    }
    public AreaLenType getLenType() {
        return lenType;
    }
    public void setLenType(AreaLenType lenType) {
        this.lenType = lenType;
    }
    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Area [areaNo=" + areaNo + ", areaName=" + areaName + ", leng=" + leng + ", dataType=" + dataType
                + ", lenType=" + lenType + ", data=" + data + "]";
    }
    
}

写两个枚举 一个表示该域的数据类型 一个表示数据长度类型

public enum AreaDataType {
    ASCII,
    BCD,
    BINARY
}


public enum AreaLenType {
    ASCII,
    BCD,
    BINARY
}

配置8583域的属性

public class BaoWenConfig {
    
    public  Area area0 = new Area(0, "msgtype", 4, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area1 = new Area(1, "拓展位图", 8, AreaDataType.BINARY, AreaLenType.BCD, false,0);
    public  Area area2 = new Area(2, "主账号", 19, AreaDataType.BCD, AreaLenType.BCD, true,2);
    public  Area area3 = new Area(3, "交易处理码", 6, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area4 = new Area(4, "交易金额", 12, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area11 = new Area(11, "系统跟踪号", 6, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area12 = new Area(12, "受卡发方所在地时间", 6, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area13 = new Area(13, "受卡发方所在地日期", 4, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area14 = new Area(14, "卡有效期", 4, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area15 = new Area(15, "清算日期", 4, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area22 = new Area(22, "服务点输入方式码", 3, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area23 = new Area(23, "卡序列号", 3, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area25 = new Area(25, "服务点条件码", 2, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area26 = new Area(26, "服务点PIN获取码", 2, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area32 = new Area(32, "受理机构标识码", 11, AreaDataType.BCD, AreaLenType.BCD, true,2);
    public  Area area35 = new Area(35, "第二磁道数据", 37, AreaDataType.BCD, AreaLenType.BCD, true,2);
    public  Area area36 = new Area(36, "第三磁道数据", 104, AreaDataType.BCD, AreaLenType.BCD, true,3);
    public  Area area37 = new Area(37, "检索参考号", 12, AreaDataType.ASCII, AreaLenType.BCD, false,0);
    public  Area area38 = new Area(38, "授权标识应答码", 6, AreaDataType.ASCII, AreaLenType.BCD, false,0);
    public  Area area39 = new Area(39, "应答码", 2, AreaDataType.ASCII, AreaLenType.BCD, false,0);
    public  Area area41 = new Area(41, "受卡机终端标识码", 8, AreaDataType.ASCII, AreaLenType.BCD, false,0);
    public  Area area42 = new Area(42, "受卡方标识码", 15, AreaDataType.ASCII, AreaLenType.BCD, false,0);
    public  Area area43 = new Area(43, "商户名称", 40, AreaDataType.ASCII, AreaLenType.ASCII, false,0);
    public  Area area44 = new Area(44, "附加响应数据", 25, AreaDataType.ASCII, AreaLenType.BCD, true,2);
    public  Area area48 = new Area(48, "附加数据-私有", 512, AreaDataType.BCD, AreaLenType.BCD, true,3);
    public  Area area49 = new Area(49, "交易货币代码", 3, AreaDataType.ASCII, AreaLenType.BCD, false,0);
    public  Area area52 = new Area(52, "个人标识码数据", 16, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area53 = new Area(53, "安全控制信息", 16, AreaDataType.BCD, AreaLenType.BCD, false,0);
    public  Area area54 = new Area(54, "实际金额", 32, AreaDataType.ASCII, AreaLenType.BCD, true,3);
    public  Area area55 = new Area(55, "基于PBOC借贷记标准的IC卡数据", 255, AreaDataType.BINARY, AreaLenType.BCD, true,3);
    public  Area area57 = new Area(57, "pos版本", 42, AreaDataType.ASCII, AreaLenType.BCD, true,3);
    public  Area area58 = new Area(58, "基于PBOC电子钱包/存折IC卡标准的交易数据", 100, AreaDataType.BINARY, AreaLenType.BCD, true,3);
    public  Area area60 = new Area(60, "自定义域", 100, AreaDataType.BCD, AreaLenType.BCD, true,3);
    public  Area area61 = new Area(61, "原始信息域", 200, AreaDataType.BCD, AreaLenType.BCD, true,3);
    public  Area area62 = new Area(62, "原始信息域", 200, AreaDataType.BINARY, AreaLenType.BCD, true,3);
    public  Area area63 = new Area(63, "金融网络数据", 200, AreaDataType.ASCII, AreaLenType.BCD, true,3);
    public  Area area64 = new Area(64, "MAC", 8, AreaDataType.ASCII, AreaLenType.BCD, false,0);
    

    public  ArrayList<Area> areas = new ArrayList<Area>(){/**
         * 
         */
        private static final long serialVersionUID = 4868905912449475521L;
    {
        add(area0);
        add(area1);
        add(area2);
        add(area3);
        add(area4);
        add(area11);
        add(area12);
        add(area13);
        add(area14);
        add(area15);
        add(area22);
        add(area23);
        add(area25);
        add(area26);
        add(area32);
        add(area35);
        add(area36);
        add(area37);
        add(area38);
        add(area39);
        add(area41);
        add(area42);
        add(area43);
        add(area44);
        add(area48);
        add(area49);
        add(area52);
        add(area53);
        add(area54);
        add(area55);
        add(area57);
        add(area58);
        add(area60);
        add(area61);
        add(area62);
        add(area63);
        add(area64);
    }};
    
    
    public Area getAreaWithAreaNo(int no){
        Area result = null;
        for (Area area : areas) {
            if(area.getAreaNo() == no){
                result = area;
                break;
            }
        }
        return result;
    }
    
    
    
}

处理8583报文的主要类

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Deal8583 {
    private static BASE64Encoder encoder = new BASE64Encoder();
    private static BASE64Decoder decoder = new BASE64Decoder();

    public static String byte2hex(byte[] b) // 二进制转字符串
    {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            
            if (stmp.length() == 1){
                hs = hs + "0" + stmp;
            }
            else{
                hs = hs + stmp;
            }
        }
        return hs;
    }
    
    public static byte[] hex2byte(String str) { // 字符串转二进制
        if (str == null){
            return null;
        }
        str = str.trim();
        int len = str.length();
        if (len == 0 || len % 2 == 1){
            return null;
        }
        byte[] b = new byte[len / 2];
        try {for (int i = 0; i < str.length(); i += 2) {
            b[i / 2] = (byte) Integer.decode("0x" + str.substring(i, i + 2)).intValue();
        }
        return b;
        } catch (Exception e) {
            return null;
        }
    }

    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 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 byteToASCString(byte[] bArray){
        String strASC="";
        String strTemp="";
        for(int i=0;i<bArray.length;i++){
            strTemp=String.format("%c",bArray[i]);
            strASC=strASC+strTemp;
        }
        return strASC;
    }

    /**
     * BASE64 编码
     * 
     * @param s
     * @return
     */
    public static String encodeBufferBase64(byte[] buff) {
        return buff == null ? null : encoder.encode(buff);
    }

    /**
     * BASE64解码
     * 
     * @param s
     * @return
     */
    public static byte[] decodeBufferBase64(String s) {
        try {
            return s == null ? null : decoder.decodeBuffer(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * BASE64 字节数组编码
     * 
     * @param s
     * @return String
     */
    public static String encodeBase64(byte[] s) {
        if (s == null)
            return null;
        String res = new BASE64Encoder().encode(s);
        res = res.replace("\n", "");
        res = res.replace("\r", "");
        return res;
    }

    /**
     * BASE64解码
     * 
     * @param s
     * @return
     */
    public static byte[] decodeBase64(byte[] buff) {
        if (buff == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(new String(buff));

            return b;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将reauest里的数据包转成字符串
     * 
     * @param request
     * @return String
     */
    public static String getRequestBodyTxt(HttpServletRequest request) {
        // 接收手机传过来的参数
        BufferedInputStream bufferedInputStream = null;
        // 此类实现了一个输出流,其中的数据被写入一个字节数组
        ByteArrayOutputStream bytesOutputStream = null;
        String body = null;
        try {
            // BufferedInputStream 输入流
            bufferedInputStream = new BufferedInputStream(request.getInputStream());
            bytesOutputStream = new ByteArrayOutputStream();
            // 写入数据
            int ch;
            while ((ch = bufferedInputStream.read()) != -1) {
                bytesOutputStream.write(ch);
            }
            // 转换为String类型
            body = new String(bytesOutputStream.toByteArray(), "UTF-8");
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // 关闭此输入流并释放与该流关联的所有系统资源。
            try {
                bytesOutputStream.flush();
                bytesOutputStream.close();
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return body;
    }

    /**
     * 将reauest里的数据包转成字节数组
     * 
     * @param request
     * @return
     */
    public static byte[] getRequestBodyByte(HttpServletRequest request) {
        // 接收手机传过来的参数
        BufferedInputStream bufferedInputStream = null;
        // 此类实现了一个输出流,其中的数据被写入一个字节数组
        ByteArrayOutputStream bytesOutputStream = null;
        byte[] body = null;
        try {
            // BufferedInputStream 输入流
            bufferedInputStream = new BufferedInputStream(request.getInputStream());
            bytesOutputStream = new ByteArrayOutputStream();
            // 写入数据
            int ch;
            while ((ch = bufferedInputStream.read()) != -1) {
                bytesOutputStream.write(ch);
            }
            // 转换为String类型
            body = bytesOutputStream.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // 关闭此输入流并释放与该流关联的所有系统资源。
            try {
                bytesOutputStream.flush();
                bytesOutputStream.close();
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return body;
    }

    public static String getEigthBitsStringFromByte(int b) {
        b |= 256; // mark the 9th digit as 1 to make sure the string
        String str = Integer.toBinaryString(b);
        int len = str.length();
        return str.substring(len - 8, len);
    }

    public static byte getByteFromEigthBitsString(String str) {
        byte b;
        if (str.substring(0, 1).equals("1")) {
            str = "0" + str.substring(1);
            b = Byte.valueOf(str, 2);
            b |= 128;
        } else {
            b = Byte.valueOf(str, 2);
        }
        return b;
    }

    /**
     * 将一个16字节数组转成128二进制数组
     * 
     * @param b
     * @return
     */
    public static boolean[] getBinaryFromByte(byte[] b) {
        boolean[] binary = new boolean[b.length * 8 + 1];
        String strsum = "";
        for (int i = 0; i < b.length; i++) {
            strsum += getEigthBitsStringFromByte(b[i]);
        }
        for (int i = 0; i < strsum.length(); i++) {
            if (strsum.substring(i, i + 1).equalsIgnoreCase("1")) {
                binary[i + 1] = true;
            } else {
                binary[i + 1] = false;
            }
        }
        return binary;
    }

    /**
     * 将一个128二进制数组转成16字节数组
     * 
     * @param binary
     * @return
     */
    public static byte[] getByteFromBinary(boolean[] binary) {

        int num = (binary.length - 1) / 8;
        if ((binary.length - 1) % 8 != 0) {
            num = num + 1;
        }
        byte[] b = new byte[num];
        String s = "";
        for (int i = 1; i < binary.length; i++) {
            if (binary[i]) {
                s += "1";
            } else {
                s += "0";
            }
        }
        String tmpstr;
        int j = 0;
        for (int i = 0; i < s.length(); i = i + 8) {
            tmpstr = s.substring(i, i + 8);
            b[j] = getByteFromEigthBitsString(tmpstr);
            j = j + 1;
        }
        return b;
    }

    /**
     * 将一个byte位图转成字符串
     * 
     * @param b
     * @return
     */
    public static String getStrFromBitMap(byte[] b) {
        String strsum = "";
        for (int i = 0; i < b.length; i++) {
            strsum += getEigthBitsStringFromByte(b[i]);
        }
        return strsum;
    }

    /**
     * bytes转换成十六进制字符串
     * 
     * @param b
     * @return
     */
    public static String byte2HexStr(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    }

    private static byte uniteBytes(String src0, String src1) {
        byte b0 = Byte.decode("0x" + src0).byteValue();
        b0 = (byte) (b0 << 4);
        byte b1 = Byte.decode("0x" + src1).byteValue();
        byte ret = (byte) (b0 | b1);
        return ret;
    }

    /**
     * 十六进制字符串转换成bytes
     * 
     * @param src
     * @return
     */
    public static byte[] hexStr2Bytes(String src) {
        int m = 0, n = 0;
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            m = i * 2 + 1;
            n = m + 1;
            ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
        }
        return ret;
    }

    /**
     * 将String转成BCD码
     * 
     * @param s
     * @return
     */
    public static byte[] StrToBCDBytes(String s) {

        if (s.length() % 2 != 0) {
            s = "0" + s;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        char[] cs = s.toCharArray();
        for (int i = 0; i < cs.length; i += 2) {
            int high = cs[i] - 48;
            int low = cs[i + 1] - 48;
            baos.write(high << 4 | low);
        }
        return baos.toByteArray();
    }

    /**
     * 将BCD码转成int
     * 
     * @param b
     * @return
     */
    public static int bcdToint(byte[] b) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            int h = ((b[i] & 0xff) >> 4) + 48;
            sb.append((char) h);
            int l = (b[i] & 0x0f) + 48;
            sb.append((char) l);
        }
        return Integer.parseInt(sb.toString());
    }

    // 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位
    public static int byteToInt(byte[] bRefArr) {
        int iOutcome = 0;
        byte bLoop;

        for (int i = 0; i < bRefArr.length; i++) {
            bLoop = bRefArr[i];
            iOutcome += (bLoop & 0xFF) << (8 * i);
        }
        return iOutcome;
    }
    
    /**
     * 向服务器 发送8583报文
     * 
     * @param send8583Str 发送给服务器的报文
     * 
     * @param host 主机地址IP
     * 
     * @param port 端口号
     * 
     * @return 返回的数据
     * */
    public static String send8583(String send8583Str,String host,int port) throws Exception{
        //客户端请求与本机在20011端口建立TCP连接 
        Socket client = new Socket(host, port);  
        client.setSoTimeout(70000);             
        //获取Socket的输出流,用来发送数据到服务端                  
        PrintStream out = new PrintStream(client.getOutputStream());          
        //获取Socket的输入流,用来接收从服务端发送过来的数据 
        InputStream buf =  client.getInputStream();  
        String str = "mpos-"+send8583Str;  
        //发送数据到服务端   
        out.println(str);  
        try{  
            byte[] b = new byte[1024];
            int rc=0;
            int c = 0;
            while( (rc = buf.read(b, c, 1024) )>=0){
                c = buf.read(b, 0, rc);
            }
            String returnStr = byte2hex(b);
            String string = returnStr;
            String str16 = string.substring(0, 4);
            int leng = Integer.parseInt(str16,16);
            String result = string.substring(0, leng*2 + 4);
            if (client!=null) {
                client.close();
            }
            return result;
        }catch(Exception e){ 
            e.printStackTrace();
            System.out.println("Time out, No response");  
        }
        if (client!=null) {
            client.close();
        }
       return null;
    }
    
    public static String bitmapHex2Binary(String bitmap){
        String bitMapStr =  bitmap;
        if (bitMapStr == null || bitMapStr.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < bitMapStr.length(); i++)
        {
            tmp = "0000"
                    + Integer.toBinaryString(Integer.parseInt(bitMapStr
                            .substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }
    
    public static String getBitMapStr(String str8583){
        return str8583.substring(30, 46);
    }
    
    /**
     * 获得报文的位图数组
     * 
     * */
    public static ArrayList<Integer> getBitMap(String bitMapStr){
        ArrayList<Integer> bitMaps = new ArrayList<Integer>();
        if (bitMapStr == null || bitMapStr.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < bitMapStr.length(); i++)
        {
            tmp = "0000"
                    + Integer.toBinaryString(Integer.parseInt(bitMapStr
                            .substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        for(int i = 0 ; i < bString.length() ; i ++){
            char c = bString.charAt(i);
            int flag = Integer.parseInt(String.format("%c", c));
            if (flag==1) {
                bitMaps.add(i+1);
            }
        }
        return bitMaps;
    }

    /**
     * 解析8583报文
     * @param str8583 8583报文
     * @param resultMap 存放8583全部解析后的Map
     * @return 填充完成的域数组集合
     * 
     * */
    public static ArrayList<Area> jiexi8583(String str8583,HashMap<String, String> resultMap){
        
        //报文总长
        int currentIndex = 0;
        String lenStr = str8583.substring(currentIndex, 4);
        resultMap.put("LEN", lenStr);
        currentIndex += lenStr.length();
        
        //tpdu
        String TPDUStr = str8583.substring(currentIndex, currentIndex+10);
        resultMap.put("TPDU", TPDUStr);
        currentIndex += TPDUStr.length();
        
        //msghead
        String MSGHEADStr = str8583.substring(currentIndex,currentIndex+12);
        resultMap.put("MSGHEAD", MSGHEADStr);
        currentIndex += MSGHEADStr.length();
        
        //报文type
        String TYPEStr = str8583.substring(currentIndex, currentIndex+4);
        resultMap.put("TYPE", TYPEStr);
        currentIndex += TYPEStr.length();
        
        //位图
        String BITMAPStr = str8583.substring(currentIndex,currentIndex+16); 
        resultMap.put("BITMAP", BITMAPStr);
        currentIndex += BITMAPStr.length();
        
        //位图数组
        ArrayList<Integer> bitmaps = getBitMap(BITMAPStr);
        
        //报文配置
        BaoWenConfig config = new BaoWenConfig();
        //根据位图找到报文对应的域
        ArrayList<Area> areas = new ArrayList<Area>();
        for (Integer areaNo : bitmaps) {
            areas.add(config.getAreaWithAreaNo(areaNo));
        }
        //为每个域填充信息
        for (Area area : areas) {
            String areaDataStr = null;//用来存放该域的数据
            if( area.isDynamicLen()){//如果是变长的话
                if(area.getDataType()==AreaDataType.BCD && area.getLenType() == AreaLenType.BCD){
                    String dylenStr = null;
                    int useLen = 0;
                    if (area.getDynamicLen_2_3() == 2) {
                        dylenStr = str8583.substring(currentIndex,currentIndex+2);
                        currentIndex += 2;
                        int dataLen = Integer.parseInt(dylenStr);
                        if (dataLen %2 ==0) {
                            useLen = dataLen; 
                        }else{
                            useLen = dataLen + 1;
                        }
                    }else if(area.getDynamicLen_2_3() == 3){
                        dylenStr = str8583.substring(currentIndex,currentIndex+4);
                        currentIndex += 4;
                        int dataLen = Integer.parseInt(dylenStr);
                        if (dataLen %2 ==0) {
                            useLen = dataLen; 
                        }else{
                            useLen = dataLen + 1;
                        }
                    }
                    
                    areaDataStr = str8583.substring(currentIndex, currentIndex+useLen);
                    currentIndex += areaDataStr.length();
                    resultMap.put(String.format("%d", area.getAreaNo()), areaDataStr);
                    area.setLeng(Integer.parseInt(dylenStr));
                    
                }else if(area.getDataType()==AreaDataType.ASCII && area.getLenType() == AreaLenType.BCD){
                    String dylenStr = str8583.substring(currentIndex,currentIndex+4);
                    currentIndex += 4;
                    areaDataStr = str8583.substring(currentIndex, currentIndex+Integer.parseInt(dylenStr)*2);
                    currentIndex +=areaDataStr.length();
                    resultMap.put(String.format("%d", area.getAreaNo()), areaDataStr);
                    
                    area.setLeng(Integer.parseInt(dylenStr));
                    
                }else if(area.getDataType()==AreaDataType.BINARY && area.getLenType() == AreaLenType.BCD){
                    String dylenStr = str8583.substring(currentIndex,currentIndex+4);
                    currentIndex += 4;
                    areaDataStr = str8583.substring(currentIndex, currentIndex+Integer.parseInt(dylenStr)*2);
                    currentIndex +=areaDataStr.length();
                    resultMap.put(String.format("%d", area.getAreaNo()), areaDataStr);
                    area.setLeng(Integer.parseInt(dylenStr));
                }
            }else{//如果是固定长度
                if(area.getDataType()==AreaDataType.BCD && area.getLenType() == AreaLenType.BCD){
                    areaDataStr = str8583.substring(currentIndex,currentIndex+area.getLeng()); 
                    resultMap.put(String.format("%d", area.getAreaNo()) , areaDataStr);
                    currentIndex += areaDataStr.length() %2 ==0 ?areaDataStr.length():areaDataStr.length()+1 ;
                }else if(area.getDataType()==AreaDataType.ASCII && area.getLenType() == AreaLenType.BCD){
                    areaDataStr = str8583.substring(currentIndex,currentIndex+area.getLeng() * 2); 
                    resultMap.put(String.format("%d", area.getAreaNo()) , areaDataStr);
                    currentIndex += areaDataStr.length();
                }
            }
            //存放到该域
            area.setData(areaDataStr);
        }
        return areas;
    }
    
    public static void main(String[] args) throws Exception {
        String baowen = "012760000300006022000000000200702406C020C09A111662596543203269930000000000008890000006972212071000010012376259654320326993D2212201000007820000003130303030303132333034343532303438313230303031313536EF7F40420E3741FE260000000000000001649F26083AE8A83B8A54F8739F2701809F101307020103A00000010A0100000000008D9318249F3704E97798B59F3602017D950500000000009A031702219C01009F02060000008890005F2A02015682027C009F1A0201569F03060000000000009F3303E0E1C89F34030200009F3501229F1E0838373937303835378408A0000003330101029F090200309F4104000006979F6310303130353030303000000000000000000014220000020006013246373437303042";
        HashMap<String,String> resultMap = new HashMap<String, String>();
        ArrayList<Area> areas = jiexi8583(baowen,resultMap);
        System.out.println(resultMap);
        for (Area area : areas) {
            System.out.println(area);
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,265评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,078评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,852评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,408评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,445评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,772评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,921评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,688评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,130评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,467评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,617评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,276评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,882评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,740评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,967评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,315评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,486评论 2 348

推荐阅读更多精彩内容