How do I pack an ISO 8583 message?

The code snippet below show you how to pack an ISO 8583 message.

package org.kodejava.example.jpos;

import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.packager.GenericPackager;

public class PackISOMessage {
    public static void main(String[] args) {
        PackISOMessage iso = new PackISOMessage();
        try {
            String message = iso.buildISOMessage();
            System.out.printf("Message = %s", message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String buildISOMessage() throws Exception {
        try {
            GenericPackager packager = new GenericPackager("fields.xml");

            ISOMsg isoMsg = new ISOMsg();
            isoMsg.setPackager(packager);
            isoMsg.setMTI("0200");

            isoMsg.set(3, "000010");
            isoMsg.set(4, "1500");
            isoMsg.set(7, "1206041200");
            isoMsg.set(11, "000001");
            isoMsg.set(41, "12340001");
            isoMsg.set(49, "840");
            printISOMessage(isoMsg);

            byte[] result = isoMsg.pack();
            return new String(result);
        } catch (ISOException e) {
            throw new Exception(e);
        }
    }

    public void printISOMessage(ISOMsg isoMsg) {
        try {
            System.out.printf("MTI = %s%n", isoMsg.getMTI());
            for (int i = 1; i <= isoMsg.getMaxField(); i++) {
                if (isoMsg.hasField(i)) {
                    System.out.printf("Field (%s) = %s%n", i, isoMsg.getString(i));
                }
            }
        } catch (ISOException e) {
            e.printStackTrace();
        }
    }
}

When you run the program you’ll get the following output:

MTI = 0200
Field (3) = 000010
Field (4) = 1500
Field (7) = 1206041200
Field (11) = 000001
Field (41) = 12340001
Field (49) = 840
Message = 02003220000000808000000010000000001500120604120000000112340001840

Maven Dependency

<dependency>
    <groupId>org.jpos</groupId>
    <artifactId>jpos</artifactId>
    <version>2.0.2</version>
</dependency>

How do I unpack an ISO 8583 message?

The code snippet below will show you how to unpack ISO 8583 message.

package org.kodejava.example.jpos;

import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.packager.GenericPackager;

public class UnpackISOMessage {
    public static void main(String[] args) {
        UnpackISOMessage iso = new UnpackISOMessage();
        try {
            ISOMsg isoMsg = iso.parseISOMessage();
            iso.printISOMessage(isoMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ISOMsg parseISOMessage() throws Exception {
        String message = "02003220000000808000000010000000001500120604120000000112340001840";
        System.out.printf("Message = %s%n", message);
        try {
            GenericPackager packager = new GenericPackager("fields.xml");
            ISOMsg isoMsg = new ISOMsg();
            isoMsg.setPackager(packager);
            isoMsg.unpack(message.getBytes());
            return isoMsg;
        } catch (ISOException e) {
            throw new Exception(e);
        }
    }

    public void printISOMessage(ISOMsg isoMsg) {
        try {
            System.out.printf("MTI = %s%n", isoMsg.getMTI());
            for (int i = 1; i <= isoMsg.getMaxField(); i++) {
                if (isoMsg.hasField(i)) {
                    System.out.printf("Field (%s) = %s%n", i, isoMsg.getString(i));
                }
            }
        } catch (ISOException e) {
            e.printStackTrace();
        }
    }
}

When you run the program you’ll get the following output:

Message = 02003220000000808000000010000000001500120604120000000112340001840
MTI = 0200
Field (3) = 000010
Field (4) = 000000001500
Field (7) = 1206041200
Field (11) = 000001
Field (41) = 12340001
Field (49) = 840

Maven Dependency

<dependency>
    <groupId>org.jpos</groupId>
    <artifactId>jpos</artifactId>
    <version>2.0.2</version>
</dependency>