[Solved] Need Java Code/Logic to extract COMP. field from EBCDIC file


I do not use Java, but I will try to explain what is happening.

The value 8188 is 0x1ff8 in big-endian hex. When your program is run the result is actually 8188 - 65536 = -57348. That is why you got the result you did.

Because the input is big-endian binary, only the first bit of b[0] should be checked for a sign. What I did in C# was,

    public static String GetBinary(byte[] b, int decimalPointLocation)
    {
        long val = 0;
        if ((b[0] & 0x80) != 0)
        {
            val = -1;
        }
        for (int i = 0; i < b.Length; i++)
        {
                val = (val << 8) + b[i];
        }
        string s = Convert.ToString(val);
        return s;
    }

For byte[] b = {0x1f, 0xfc} the value returned is 8188. For byte[] b = {0xe0, 0x04} the value returned is -8188.

While I did use similar substring manipulation to insert a decimal point for those values, you need to be aware that absolute integer values less than 100 cannot be properly formatted for two decimal places with that method.

solved Need Java Code/Logic to extract COMP. field from EBCDIC file