在 ASCII 转换中解包 EBCDIC 压缩小数 (COMP-3)

发布于 2024-08-23 21:32:49 字数 276 浏览 7 评论 0原文

我正在使用 Jon Skeet 在 .NET 中的 EBCDIC 实现 读取以二进制模式下载的 VSAM 文件通过大型机系统的 FTP。它非常适合在此编码中读取/写入,但它没有任何读取打包十进制值的功能。我的文件包含这些,我需要解压它们(显然,以更多字节为代价)。

我该怎么做?

我的字段定义为 PIC S9(7)V99 COMP-3。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

歌入人心 2024-08-30 21:32:49

啊,BCD。如果您在 6502 装配中使用过它,请鸣喇叭。

当然,最好的选择是让 COBOL MOVE 为您完成这项工作!这些可能性之一可能会有所帮助。

(可能性#1)假设您确实有权访问主机和源代码,并且输出文件仅供您使用,请修改程序,使其仅将值移动到普通未打包的 PIC S9(7)V99。

(可能性#2)假设这不是那么容易(例如,文件是其他 pgms 的输入,或者无法更改代码),您可以在系统上编写另一个 COBOL 程序来读取该文件并写入另一个文件。使用 BCD 将文件记录布局剪切并粘贴到输入和输出文件的新程序中。修改输出版本为非打包版本。读取一条记录,进行“移动对应”传输数据,然后写入,直到eof。然后传输文件。

(可能性#3)如果您无法触摸主机,请注意您在评论中链接的文章中的描述。 BCD 码比较简单。它可能就像这样简单(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

我用这个调用的一些变体测试了它:

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

例如,is -40.1。但只有几个。所以它可能仍然是错误的。

因此,如果您的 comp-3 从输入记录布局的字节 10 开始,这将解决该问题:

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

注意数据转换文章中关于要发送的字节数的公式,以及之前和之前的 9 的数量将

结果存储在小数中以避免舍入错误。特别是如果它是$$$。浮动&双倍会让你悲伤!如果您不处理它,即使是字符串也更好。

当然可能会更难。我工作的地方,大型机每字节 9 位。严肃的。这就是前两种可能性如此突出的原因。当然,真正让它们变得更好的是,您可能只是一名仅使用 PC 的程序员,这是让大型机程序员为您完成工作的一个很好的借口!如果你很幸运有这样的选择......

和平,
-铝

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

和影子一齐双人舞 2024-08-30 21:32:49

我使用此扩展方法进行压缩十进制 (BCD) 转换:

    /// <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;
    }

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(
                
quot;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(
quot;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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文