我用ISO 8583 v87创建一条消息!但是在解析它时,我得到不同的参数值。问题是什么?适用于Hex.encodeHexString
import org.apache.commons.codec.binary.Hex;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
public class ParseISOMessage {
static org.jpos.iso.packager.ISO87APackager packager;
public static void main(String[] args) throws IOException, ISOException {
String data = createMSG();
// Create Packager based on XML that contain DE type
packager = new org.jpos.iso.packager.ISO87APackager();
// Create ISO Message
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.unpack(data.getBytes());
// print the DE list
logISOMsg(isoMsg);
}
private static void logISOMsg(ISOMsg msg) {
System.out.println("----ISO MESSAGE-----");
try {
System.out.println(" MTI : " + msg.getMTI());
for (int i=1;i<=msg.getMaxField();i++) {
if (msg.hasField(i)) {
System.out.println(" Field-"+i+" ("+packager.getFieldPackager(i).getDescription()+") : "+msg.getString(i));
}
}
} catch (ISOException e) {
e.printStackTrace();
} finally {
System.out.println("--------------------");
}
}
private static String createMSG() {
try{
ISO87BPackager packager = new ISO87BPackager();
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.setMTI("0200");
isoMsg.set(2, "1234567890123456");
isoMsg.set(3, "000000");
isoMsg.set(4,"000000001234");
isoMsg.set(7, "0413135553");
isoMsg.set(11, "000091");
isoMsg.set(12, "135553");
isoMsg.set(13, "0413");
isoMsg.set(25, "14");
isoMsg.set(32, "5816720610");
isoMsg.set(35, "1234567890123456=9505146000000000000");
isoMsg.set(37, "000000000000");
isoMsg.set(41,"55555555");
isoMsg.set(49,"123");
isoMsg.set(52,"1A2B3C4D5E6F7012");
isoMsg.set(64,"ABCDEF0123456789");
byte[] b = isoMsg.pack();
System.out.println("**"+Hex.encodeHexString(b)+"**");
return Hex.encodeHexString(b);
}
catch(Exception ex) {
ex.printStackTrace();
return "";
}
}
}
例如,我将 PIN DATA 设置为ABCDEF0123456789
,但在解析创建的消息时, PIN DATA 为0353535353535353
。
答案 0 :(得分:1)
当您致电data.getBytes()
数据实际上是编码的十六进制数据而不是isoMsg.pack()
创建的数据时,问题非常简单。
更改isoMsg.unpack(data.getBytes())
到
final byte[] bytes = new Hex().decode(data.getBytes());
isoMsg.unpack(bytes);
此外,您应该使用相同的packager
来打包和解压缩
public class ParseISOMessage {
private static org.jpos.iso.packager.ISO87APackager packager = new org.jpos.iso.packager.ISO87APackager();
public static void main(String[] args) throws IOException, ISOException, DecoderException {
String data = createMSG();
// Create ISO Message
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
final byte[] bytes = new Hex().decode(data.getBytes());
isoMsg.unpack(bytes);
...
private static String createMSG() {
try {
// ISO87BPackager packager = new ISO87BPackager(); // delete this line otherwise it cannot unpack
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.setMTI("0200");
...//same
isoMsg.set(64, "ABCDEF0123456789");
byte[] b = isoMsg.pack();
System.out.println("**" + new String(Hex.encodeHex(b, false)) + "**"); // make the result uppercase.
return Hex.encodeHexString(b);
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
}