1. 一:IS08583包介绍: 
  2.  
  3.      ISO8583包(简称8583包)是一个国际标准的包格式,最多由128个字段域组成,每个域都有统一的规定,并有定长与变长之分。 
  4. 8583包前面一段为位图,用来确定包的字段域组成情况。其中位图是8583包的灵魂,它是打包解包确定字段域的关键, 而了解每个字段域的属性则是填写数据的基础。    
  5.  
  6. 1:位图说明: 
  7.  
  8.      位置:在8583包的第1 位 
  9.      格式:定长 
  10.      类型:B16(二进制16位,16*8=128bit) 
  11.      描述:   
  12.      如将位图的第一位设为'1',表示使用扩展位图(128个域),否则表示只使用基本位图(64个域)。   
  13.       如使用某数据域,应在位图中将相应的位设位'1',如使用41域,需将位图的41位设为'1'。   
  14.      选用条件:如使用65到128域,需设位图域第一位为'1'   
  15. 2:域的定义: 
  16. typedef struct ISO8583 
  17. int bit_flag;            /*域数据类型0 -- string, 1 -- int, 2 -- binary*/ 
  18. char *data_name; /*域名*/ 
  19. int length;             /*数据域长度*/ 
  20. int length_in_byte;/*实际长度(如果是变长)*/ 
  21. int variable_flag; /*是否变长标志0:否 2:2位变长, 3:3位变长*/ 
  22. int datatyp;         /*0 -- string, 1 -- int, 2 -- binary*/ 
  23. char *data;        /*存放具体值*/ 
  24. int attribute;      /*保留*/ 
  25. } ISO8583; 
  26.  
  27. 二:定义BitMap类 
  28.  
  29. 类说明:根据ISO8583 包的域定义,定义BitMap类存储每个域的信息。例如: 
  30.  
  31. package com.lottery.pos.model; 
  32.  
  33. public class BitMap { 
  34. private int bit; //位 
  35. private int bittype; //数据类型 1 ascii 2 binary 
  36. private int variable; //是否变长0 不是 2 两位变长 3 三位变长 
  37. private int len; //数据长度 
  38. private byte[] dat; //数据 
  39.  
  40. public int getBit() { 
  41.    return bit
  42. public void setBit(int bit) { 
  43.    this.bit = bit
  44. public int getBittype() { 
  45.    return bittype; 
  46. public void setBittype(int bittype) { 
  47.    this.bittype = bittype; 
  48. public int getVariable() { 
  49.    return variable; 
  50. public void setVariable(int variable) { 
  51.    this.variable = variable; 
  52. public byte[] getDat() { 
  53.    return dat; 
  54. public void setDat(byte[] dat) { 
  55.    this.dat = dat; 
  56. public int getLen() { 
  57.    return len; 
  58. public void setLen(int len) { 
  59.    this.len = len; 
  60.  
  61.  
  62.  
  63. 三:定义PortConfig类 
  64.  
  65. 类说明:定义配置信息类。根据此类解析和封装数据。例如: 
  66.  
  67. package com.lottery.pos.model; 
  68.  
  69. public class PortConfig { 
  70. /** 
  71. * 存放所有接口的配置信息 
  72. * [][0] bit     位:在Map中的位 
  73. * [][1] type   类型:1 ascii 2 binary 
  74. * [][2] len    长度:(对定长有效) 
  75. * [][3] varLen 变长:0非变长 2位变长 3位变长 
  76. */ 
  77. // 定义一个二位数组存放配置信息。 
  78. public static final int[][] config= { 
  79.     {11,1,6,0}, 
  80.     {12,1,6,0}, 
  81.     {13,1,4,0}, 
  82.     {32,1,11,0}, 
  83.     {37,1,12,0}, 
  84.     {39,1,2,0}, 
  85.     {40,2,50,2}, 
  86.     {41,1,8,0}, 
  87.     {48,1,52,3}, 
  88.     {120,2,128,3}, 
  89.     }; 
  90.  
  91. } 
  92.  
  93. 四:定义BitMapiso类 
  94.  
  95. 类说明:此类提供解析请求包和封装信息包两个方法,例如: 
  96.  
  97. package com.lottery.pos.utils; 
  98.  
  99. import java.util.ArrayList; 
  100. import java.util.List; 
  101.  
  102. import com.lottery.pos.model.BitMap; 
  103.  
  104. public class BitMapiso { 
  105.  
  106. /** 
  107. * 解析请求包 
  108. * @param body 
  109. * @param config 
  110. * @return List 
  111. */ 
  112. @SuppressWarnings("unchecked"
  113. public static List unpackRequest(byte[] body, int[][] config) { 
  114.    List outList = new ArrayList(); 
  115.    // 取得除信息类型以外的包信息。也就是取得位图的初始位置。 
  116.    byte[] realbody = new byte[body.length - 4]; 
  117.    System.arraycopy(body, 4, realbody, 0, realbody.length); 
  118.    // 取得位图 
  119.    byte[] map = null
  120.    byte[] map8 = new byte[8]; 
  121.    System.arraycopy(realbody, 0, map8, 0, 8); 
  122.    boolean[] bmap8 = LoUtils.getBinaryFromByte(map8); 
  123.    if (bmap8[1]) { 
  124.    // 如果第一位为1,则是可扩展位图,设为16字节长度。 
  125.     map = new byte[16]; 
  126.     System.arraycopy(realbody, 0, map, 0, 16); 
  127.    } else { 
  128.     map = map8; 
  129.    } 
  130.    boolean[] bmap = LoUtils.getBinaryFromByte(map); 
  131.  
  132.    int tmplen = map.length; 
  133.    for (int i = 2; i < bmap.length; i++) { 
  134.     if (bmap[i]) { 
  135.      //BitMap bitMap = null
  136.      // 寻找位图中的1对应的数据 
  137.      int bit=-1; 
  138.      for (int j = 0; j < config.length; j++) { 
  139.       if (config[j][0] == i) { 
  140.        bit=j; 
  141.        break; 
  142.       } 
  143.      } 
  144.      BitMap outBitMap = new BitMap(); 
  145.      outBitMap.setBit(i); 
  146.      outBitMap.setBittype(config[bit][1]); 
  147.      //len对变长是无用的。 
  148.      outBitMap.setLen(config[bit][2]); 
  149.      outBitMap.setVariable(config[bit][3]); 
  150.      byte[] nextData = null
  151.      if (config[bit][3] > 0) { 
  152.       //取出变长部分的值。 
  153.       int varLen = config[bit][3]; 
  154.       if (config[bit][1] == 2) { 
  155.        varLen = varLen - 1; 
  156.       } 
  157.       byte[] varValue = new byte[varLen]; 
  158.       System.arraycopy(realbody, tmplen, varValue, 0, varValue.length); 
  159.       int datLen = 0; 
  160.       if (config[bit][1] == 2) { 
  161.        datLen = LoUtils.bcdToint(varValue); 
  162.       } else { 
  163.        datLen = byteToInt(varValue); 
  164.       } 
  165.  
  166.       tmplen += varLen; 
  167.       // 取出变长部分后带的值。 
  168.       nextData = new byte[datLen]; 
  169.  
  170.       System.arraycopy(realbody, tmplen, nextData, 0,nextData.length); 
  171.       tmplen += nextData.length; 
  172.      } else { 
  173.       nextData = new byte[config[bit][2]]; 
  174.       System.arraycopy(realbody, tmplen, nextData, 0,nextData.length); 
  175.       tmplen += config[bit][2]; 
  176.      } 
  177.      outBitMap.setDat(nextData); 
  178.      outList.add(outBitMap); 
  179.     } 
  180.    } 
  181.  
  182.    return outList; 
  183.  
  184. /** 
  185. * 打包响应包,不包括消息类型 
  186. * @param list 
  187. * @return byte[] 
  188. */ 
  189. @SuppressWarnings("unchecked"
  190. public static byte[] PackResponse(List list) { 
  191.    int len = 16; 
  192.    for (int i = 0; i < list.size(); i++) { 
  193.     BitMap bitMap = (BitMap) list.get(i); 
  194.     // 计算请求包总长度 
  195.     if (bitMap.getBittype() == 2) { 
  196.      if (bitMap.getVariable() > 0) { 
  197.       len += bitMap.getVariable() - 1 + bitMap.getDat().length; 
  198.      } else { 
  199.       len += bitMap.getVariable() + bitMap.getDat().length; 
  200.      } 
  201.     } else { 
  202.      len += bitMap.getVariable() + bitMap.getDat().length; 
  203.     } 
  204.    } 
  205.    byte[] body = new byte[len]; 
  206.    // 位图 
  207.    boolean[] bbitMap = new boolean[129]; 
  208.    bbitMap[1] = true
  209.    int temp = (bbitMap.length - 1) / 8; 
  210.    for (int j = 0; j < list.size(); j++) { 
  211.     BitMap bitMap = (BitMap) list.get(j); 
  212.     bbitMap[bitMap.getBit()] = true
  213.     byte[] bitmap = LoUtils.getByteFromBinary(bbitMap); 
  214.     System.arraycopy(bitmap, 0, body, 0, bitmap.length); 
  215.     // 数据 
  216.     if (bitMap.getVariable() > 0) { 
  217.      // 数据是可变长的:拼变长的值 
  218.      byte[] varValue = null
  219.      if (bitMap.getBittype() == 2) { 
  220.       varValue = LoUtils.StrToBCDBytes(String.format("%0"+ bitMap.getVariable() + "d",bitMap.getDat().length)); 
  221.      } else { 
  222.       varValue = String.format("%0" + bitMap.getVariable() + "d",bitMap.getDat().length).getBytes(); 
  223.      } 
  224.      System.arraycopy(varValue, 0, body, temp, varValue.length); 
  225.      temp += varValue.length; 
  226.      // 拼变长部分后所带的数的值。 
  227.      System.arraycopy(bitMap.getDat(), 0, body, temp, bitMap.getDat().length); 
  228.      temp += bitMap.getDat().length; 
  229.     } else { 
  230.      // 数据是固定长度的。 
  231.      byte dat[] =new byte[bitMap.getLen()]; 
  232.      if (bitMap.getDat().length!=bitMap.getLen()){      
  233.       System.arraycopy(bitMap.getDat(), 0, dat, 0, bitMap.getLen()); 
  234.      }else
  235.       dat=bitMap.getDat(); 
  236.      } 
  237.      System.arraycopy(dat, 0, body, temp, dat.length); 
  238.      temp += bitMap.getDat().length; 
  239.     } 
  240.    } 
  241.    return body; 
  242.  
  243. }