Unpacking EBCDIC Packed Decimals (COMP-3) in an ASCII Conversion
Asked Answered
B

2

7

I am using Jon Skeet's EBCDIC implementation in .NET to read a VSAM file downloaded in binary mode with FTP from a mainframe system. It works very well for reading/writing in this encoding, but it does not have anything to read packed-decimal values. My file contains these, and I need to unpack them (at the cost of more bytes, obviously).

How can I do this?

My fields are defined as PIC S9(7)V99 COMP-3.

Bucket answered 1/3, 2010 at 14:26 Comment(4)
Is packed-decimal part of the EBCDIC format, or is this just a file which happens to use both? I'm not familiar with that format, I'm afraid :(Erg
It's a file that uses both. It's an old school way to squeeze numbers to save bytes. a -99999.99 can be stored in 4 bytes.Bucket
3480-3590-data-conversion.com/article-packed-fields.htmlBucket
@ T.E.D. Haha thanks! It is kind of funny to still go to such efforts to save a few bytes, but when you have millions of records I guess it adds up.Bucket
A
4

Ahh, BCD. Honk if you used it in 6502 assembly.

Of course, the best bet is to let the COBOL MOVE do the job for you! One of these possibilities may help.

(Possibility #1) Assuming you do have access to the mainframe and the source code, and the output file is ONLY for your use, modify the program so it just MOVEs the value to a plain unpacked PIC S9(7)V99.

(Possibility #2) Assuming it's not that easy, (e.g., file is input for other pgms, or can't change the code), you can write another COBOL program on the system that reads that file and writes another. Cut and paste the file record layout with the BCD into the new program for input and output files. Modify the output version to be non-packed. Read a record, do a 'move corresponding' to transfer the data, and write, until eof. Then transfer that file.

(Possibility #3) If you can't touch the mainframe, note the description in the article you linked in your comment. BCD is relatively simple. It could be as easy as this (vb.net):

Private Function FromBCD(ByVal BCD As String, ByVal intsz As Integer, ByVal decsz As Integer) As Decimal
    Dim PicLen As Integer = intsz + decsz
    Dim result As Decimal = 0
    Dim val As Integer = Asc(Mid(BCD, 1, 1))
    Do While PicLen > 0
        result *= 10D
        result += val \ 16
        PicLen -= 1
        If PicLen > 0 Then
            result *= 10D
            result += val Mod 16
            PicLen -= 1
            BCD = Mid(BCD, 2)
        End If
        val = Asc(Mid(BCD, 1, 1))
    Loop
    If val Mod 16 = &HD& Then
        result = -result
    End If
    Return result / CDec(10 ^ decsz)
End Function

I tested it with a few variations of this call:

MsgBox(FromBCD("@" & Chr(13 + 16), 2, 1))

E.g., is -40.1. But just a few. So it might still be wrong.

So then if your comp-3 starts, say, at byte 10 of the input record layout, this would solve it:

dim valu as Decimal = FromBCD(Mid(InputLine,10,5), 7,2))

Noting the formulas from the data-conversion article for the # of bytes to send in, and the # of 9's before and after the V.

Store the result in a Decimal to avoid rounding errors. Esp if it's $$$. Float & Double WILL cause you grief! If you're not processing it, even a string is better.

of course it could be harder. Where I work, the mainframe is 9 bits per byte. Serious. That's what makes the first 2 possibilities so salient. Of course what really makes them better is the fact the you may be a PC only programmer and this is a great excuse to get a mainframe programmer to do the work for you! If you are so lucky to have that option...

Peace, -Al

Accelerant answered 5/3, 2010 at 20:20 Comment(4)
Thanks Al! It may be possible to get the COBOL program changed or have another one written to unpack, but the goal is to accomplish this in .NET so we can just run straight VSAM -> FTP tasks. How do I know where the V (decimal) is at based on the binary representation? Is that even possible? How does the mainframe know? Thanks again, I appreciate your help.Bucket
Josh, you can't know where the V is from just the data; you need the PIC. The mainframe keeps track of it from the PIC. Typical lean, bit-saving solution from the punchcard days. But if you think about it, there is a never a case where the system won't have the info it needs. Heck, if you can't see the record layout yourself, you're going to have to hack it all anyway!Accelerant
Yeah, you're right, I didn't think there would be a way. I do have all of the offsets determined and the record layout is here, I was just curious if there was a way to figure out the decimal position programmatically. Okay, I will give this function a whirl tomorrow and let you know how it works out! Thanks again for your help here, I wish I could upvote twice.Bucket
Thanks again for this answer! Over six years later and still useful.Bucket
L
0

I use this extension method for packed decimal (BCD) conversion:

    /// <summary>
    /// computes the actual decimal value from an IBM "Packed Decimal" 9(x)v99 (COBOL COMP-3) format
    /// </summary>
    /// <param name="value">byte[]</param>
    /// <param name="precision">byte; decimal places, default 2</param>
    /// <returns>decimal</returns>
    public static decimal FromPackedDecimal(this byte[] value, byte precision = 2)
    {
        if (value.Length < 1)
        {
            throw new System.InvalidOperationException("Cannot unpack empty bytes.");
        }
        double power = System.Math.Pow(10, precision);
        if (power > long.MaxValue)
        {
            throw new System.InvalidOperationException(
                $"Precision too large for valid calculation: {precision}");
        }
        string hex = System.BitConverter.ToString(value).Replace("-", "");
        var bytes = Enumerable.Range(0, hex.Length)
                 .Select(x => System.Convert.ToByte($"0{hex.Substring(x, 1)}", 16))
                 .ToList();
        long place = 1;
        decimal ret = 0;
        for (int i = bytes.Count - 2; i > -1; i--)
        {
            ret += (bytes[i] * place);
            place *= 10;
        }
        ret /= (long)power;
        return (bytes.Last() & (1 << 7)) != 0 ? ret * -1 : ret;
    }
Laliberte answered 21/1, 2021 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.