Convert String to packed decimal in java

910 Views Asked by At

I have a use case wherein I need to create a file for the mainframe, which contains text and packed decimal using Java.

I have gone through a lot of threads on Stack Overflow, but nothing seems to be working. I am using JTOpen library.

Here is a sample program I wrote:

package com.amazonaws.samples.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.StringJoiner;

import com.ibm.as400.access.AS400PackedDecimal;

public class PackedDecimalTest {

    public static void main(String args[]) throws IOException {
        AS400PackedDecimal packedDecimal = new AS400PackedDecimal(8, 0);
        BigDecimal javaBigDecimal = new BigDecimal("20200521");
        byte[] convertedBytesArray = packedDecimal.toBytes(javaBigDecimal);

        try (FileOutputStream fos = new FileOutputStream("test.txt", false)) {

            //StringJoiner joiner = new StringJoiner(" ");
            //String firstName = new String("firstName1");
            //String lastName = new String("lastName1");
            //String empId = new String("1111");
            //String dept = new String("empDept1");
            //String n = new String("N");
            //joiner = joiner.add(firstName).add(lastName).add(empId).add(dept).add(n);
            //fos.write(joiner.toString().getBytes());
            fos.write(convertedBytesArray);

        }

        try (FileInputStream fis = new FileInputStream("test.txt")) {
            BigDecimal convertedBigDecimal = (BigDecimal) packedDecimal.toObject(fis.readAllBytes());
            System.out.println(convertedBigDecimal.toString());
        }
    }

}

When I write the file and read it immediately, it seems to be working fine in Java. I was able to see the output properly.

However, when the same file is opened in the mainframe, I don't see packed decimal data properly.

Here is how I see data in the mainframe:

mainframe file

I'm pretty much stuck on what needs to be done to fix it. Any pointers on what I am doing wrong? How can I write both ASCII and packed decimal data in the same file? Do I need to define any character set before writing a file for the mainframe to properly read data? Can I write it in a .txt file, or the file extension should be different or the extension have no significance? I have no knowledge on mainframe and file formats and character sets it supports.

2

There are 2 best solutions below

2
Hogstrom On

Given that packed decimal is effectively a "binary" value you'll need to create the file with the encoding of the mainframe (CP-037 or CP-1047 or what is your preferred codepage) and the packed decimal "binary" fields and then transfer the file as binary. I'm not familiar with the code pages used for AS/400 but its the same issue regardless.

If you use scp the file will undergo an implicit translation which will corrupt the file.

Use FTP / FTPS using binary mode for the transfer.

Here is a link on an approach to convert from one codepage to another.

Converting String from One Charset to Another

3
Bruce Martin On

The corruption you are seeing is from converting an ASCII file to EBCDIC. You need to write the file as EBCDIC and do a binary (straight no translation) transfer to the mainframe.

Following from @Hogstroms answer,

Change

        fos.write(joiner.toString().getBytes());

to

        fos.write(joiner.toString().getBytes("cp037"));

This will write the file as EBCDIC. You will need to change the Mainframe transfer to binary.


You may find JRecord useful, particularly if you have a cobol Copybook.