0

How can I convert an EBCDIC encoded file with PIC S9(04) COMP. to an integer value?

This file contains 1234 in the first line and -1234 on the second line.

Binary:

0101100011010010101100000101000011101111011000001011000001010000

Example:

enter image description here

aybe
  • 15,516
  • 9
  • 57
  • 105
LGL
  • 1
  • 1
  • 1
    Does this answer your question? [How to convert from EBCDIC to ASCII in C#.net](https://stackoverflow.com/questions/2858202/how-to-convert-from-ebcdic-to-ascii-in-c-net). By the way, as soon as you would have entered _"Ebcdic COMP value to Integer"_ SO would have responded with suggestions thus saving you from having to ask a question. :) –  May 07 '21 at 23:59
  • Im good with just converting EBCDIC to ACII, but its works totally different in COMP, COMP-3 data types. – LGL May 08 '21 at 00:28

2 Answers2

2

How can I convert an EBCDIC encoded file with PIC S9(04) COMP. to an integer value?

Don't do that.

During file conversion, the binary data is converted using an EBCDIC to ASCII table. The 04 D2 (+1234) was converted to 1A 4B. The 04 (EBCDIC SEL) was changed to 1A (ASCII SUB), because there is no equivalent for the conversion. The D2 (EBCDIC 'K') was changed to 4B (ASCII 'K'). There is no way to reverse the conversion, due to the SUB. The same problem exists with the conversion of -1234.

Your best bet is to use PIC +9(04), which will be +1234 or -1234 both before and after conversion.


Test data using PIC +9(04) written to file

+1234
-1234
+0001
-0001
+0000

Test program

using System;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "z:e1.txt";
            using (StreamReader sr = new StreamReader(path))
            {
                while (sr.Peek() >= 0)
                {
                    String s = sr.ReadLine();
                    short n = Int16.Parse(s);
                    Console.WriteLine(n);
                }
            }
        }
    }
}

Displayed result

1234
-1234
1
-1
0
Rick Smith
  • 3,962
  • 6
  • 13
  • 24
  • 2
    Yes - or transfer the file in binary mode so it is unchanged - which allows you to do the decoding correctly (with the additional need to convert the EBCDIC part) – Simon Sobisch May 08 '21 at 11:43
  • @Rick Smith So how does COBOL converts this to non comp type? Is there an algorithm how COBOL handles this data type? I have COMP-3, Singed fields figured out, they all are working. Im stuck on this COMP type. – LGL May 11 '21 at 12:40
  • @LGL - Normally, conversion of data from any `USAGE COMP` type is done with a `MOVE` statement with the receiving field being defined as (or by default) `USAGE DISPLAY`. A utility is used to convert the file from EBCDIC to ASCII. Because `COMP-3` is not EBCDIC encoded character data, it should always be converted to display before an EBCDIC to ASCII conversion; not "figured out." – Rick Smith May 11 '21 at 15:15
1

COBOL data types with computational usage (COMP, COMP-n) are not text, so you must not do any character conversion operation with them.

Computational types are stored as either binary numbers, floating point numbers, or packed decimal numbers. COMP is binary. The length depends on the number of 9s in the PIC clause. Note that the binary types are storeed in big-endian format.

In your case, the PIC +9(4) occupies two bytes, each. For example. if you see

0x1234

as the hexadecimal represetation of a PIC +9(4) COMP field in the input data, this is representing the number (0x12 * 0x100) + 0x34, in decimal (18 * 256) + 52 = 4608 + 52 = 4660.

In summary, mixed data (records) containing fields of types PIC ... USAGE DISPLAY and PIC ... USAGE COMP-n must never be translated, but must be split into indidual fields, and each field must be handled according to its USAGE type.

phunsoft
  • 2,674
  • 1
  • 11
  • 22