program to unpack packed data

This is a discussion on program to unpack packed data within the C Programming forums, part of the General Programming Boards category; Hi, I get a file from mainframe the format of which is f1f2f3p1p2f4p3 where f1,f2,f3,f4 are character fields and p1,p2,p3 ...

  1. #1
    vsk
    vsk is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    program to unpack packed data

    Hi,
    I get a file from mainframe the format of which is
    f1f2f3p1p2f4p3
    where f1,f2,f3,f4 are character fields
    and p1,p2,p3 are packed data in comp-3 format.

    I dont know how to unpack the packed data.

    Can anyone help me out with this.

    Thanks,
    Vinayak

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Here's a site google turned up. Aren't search engines great?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    vsk
    vsk is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    Hi,
    I tried that code but it doen'd seem to be working.
    Do you know of any C code that does it.
    I need to get the Pic S9(4)V(4)COMP-3 data as a regular number. And this packed fields are in between the character fields.I should be able to read these packed data in a readable format and write it to a output file.


    Thanks,
    VSK

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just a thought.... if you're receiving packed data fields from a mainframe, they chances are the text fields will be in EBCDIC. When you do the FTP (or whatever method you use), you must transmit in binary mode in order to keep the packed field safe.

    This results in you having an EBCDIC file on your PC (or whatever it is you're on), which may be running ASCII by default. So, you'll have some conversion work to do.

    You probably know this already.... just thought I'd point it out anyway.

    [edit]
    Post some sample data (in hex format)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    vsk
    vsk is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    Here is the java code that i am using

    The first program converts comp-3 to unpacked ebcdiic format and the second one translates ebcdiic to ascii
    One more thing is that these packed fields are placed in between the normal character fields.
    How do I segregate both of them.

    Also does any one know of any c-program which does this.

    import java.util.*;
    import java.io.*;

    public final class HexByte {
    private static File input_file;
    private static FileInputStream input;
    private static File output_file;
    private static FileOutputStream output;
    private static byte hexbyte;

    /**
    * Description: base 10 int changed to two 4 bit BitSet's
    * @param byte the byte the BitSet's are to be created from
    * @return BitSet[] the BitSet's created from the byte
    * @exception
    */
    private final static BitSet[] bitSetsFromBase10(byte hi) {
    BitSet bits[] = new BitSet[2];
    bits[0] = new BitSet(4);
    bits[1] = new BitSet(4);
    int bb = new Integer(hi).intValue();
    int m7 = bb / 128;
    bb = (bb-(m7*128));
    if (m7 > 0) { bits[0].set(3); }
    int m6 = bb / 64;
    bb = (bb-(m6*64));
    if (m6 > 0) { bits[0].set(2); }
    int m5 = bb / 32;
    bb = (bb-(m5*32));
    if (m5 > 0) { bits[0].set(1); }
    int m4 = bb / 16;
    bb = (bb-(m4*16));
    if (m4 > 0) { bits[0].set(0); }
    int m3 = bb / 8;
    bb = (bb-(m3*8));
    if (m3 > 0) { bits[1].set(3); }
    int m2 = bb / 4;
    bb = (bb-(m2*4));
    if (m2 > 0) { bits[1].set(2); }
    int m1 = bb / 2;
    bb = (bb-(m1*2));
    if (m1 > 0) { bits[1].set(1); }
    int m0 = bb;
    if (m0 > 0) { bits[1].set(0); }
    return(bits);
    }
    /**
    * Description: bit settings changed to base 10
    * @param BitSet the BitSet the decimal integer is to be created from
    * @return bb the int created from the BitSet
    * @exception
    */
    private final static int intBase10FromBitSet(BitSet bs) {
    // bit settings changed to base 10
    boolean m0 = bs.get(0);
    boolean m1 = bs.get(1);
    boolean m2 = bs.get(2);
    boolean m3 = bs.get(3);

    int bb = 0;
    if (m0) { bb = bb + 1; }
    if (m1) { bb = bb + 2; }
    if (m2) { bb = bb + 4; }
    if (m3) { bb = bb + 8; }
    return(bb);
    }
    /**
    * Description: bit settings changed to base 10
    * @param BitSet the BitSet the zoned format decimal integer is to be created from
    * @return bb the int created from the BitSet
    * @exception
    */
    private final static int intBase10ZonedFromBitSet(BitSet bs) {
    boolean m0 = bs.get(0);
    boolean m1 = bs.get(1);
    boolean m2 = bs.get(2);
    boolean m3 = bs.get(3);

    int bb = 0;
    if (m0) { bb = bb + 1; }
    if (m1) { bb = bb + 2; }
    if (m2) { bb = bb + 4; }
    if (m3) { bb = bb + 8; }
    if (bb < 10) {
    bb = bb + 240;
    } else if (bb == 10) { /* positive */
    bb = bb + 240;
    } else if (bb == 11) { /* negative */
    bb = bb + 208;
    } else if (bb == 12) { /* positive */
    bb = bb + 240;
    } else if (bb == 13) { /* negative */
    bb = bb + 208;
    } else if (bb == 14) { /* positive */
    bb = bb + 240;
    } else if (bb == 15) { /* positive */
    bb = bb + 240;
    }
    return(bb);
    }

    public static void main(String args[]) {
    try {
    input_file = new File("input.file");
    input = new FileInputStream(input_file);
    output_file = new File("output.file");
    output = new FileOutputStream(output_file);
    } catch (Exception e) {
    System.out.println("Something went wrong with the files " + e);
    }
    try {
    /* simple 16 bit - 2 bytes packed decimal converted to zoned format decimal integer */
    /* 123C (positive) converted to F1F2 F3 or 123D (negitive) converted to F1F2 D3 */

    toZoned();
    withSign();

    } catch (Exception e) {
    System.out.println("Something went wrong with reading the file " + e);
    }
    }

    private static byte readByte() throws IOException {
    int ch = input.read();
    if (ch < 0)
    throw new EOFException();
    return (byte)(ch);

    }
    /**
    * Description: both sets of 4 bits in an 8 bit byte converted to zoned format decimal integer
    */
    private static void toZoned() throws IOException {
    hexbyte = readByte();
    BitSet these_bits[] = bitSetsFromBase10(hexbyte);
    int b1 = intBase10ZonedFromBitSet(these_bits[0]);
    output.write(b1);
    int b2 = intBase10ZonedFromBitSet(these_bits[1]);
    output.write((int)b2);
    }
    /**
    * Description: left most 4 bits in an 8 bit byte converted to zoned format decimal integer
    * right most 4 bits in an 8 bit byte converted to zoned format sign
    */
    private static void withSign() throws IOException {
    hexbyte = readByte();
    System.out.println("Read in byte " + hexbyte);
    BitSet those_bits[] = bitSetsFromBase10(hexbyte);
    int b1 = intBase10FromBitSet(those_bits[0]);
    int b2 = intBase10FromBitSet(those_bits[1]);
    System.out.println(b1 + " sign byte is " + b2);
    if (b2 == 10) { /* positive */
    b1 = b1 + 240;
    } else if (b2 == 11) { /* negative */
    b1 = b1 + 208;
    } else if (b2 == 12) { /* positive */
    b1 = b1 + 240;
    } else if (b2 == 13) { /* negative */
    b1 = b1 + 208;
    } else if (b2 == 14) { /* positive */
    b1 = b1 + 240;
    } else if (b2 == 15) { /* positive */
    b1 = b1 + 240;
    }
    output.write((int)b1);
    }
    }

    import java.util.*;
    import java.io.*;
    public class Translate {
    private static File input_file;
    private static FileInputStream input;
    private static File output_file;
    private static FileOutputStream output;
    private static byte b1;


    private static final int SIZE = 95;

    private static final int ASCII[] = {
    0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028,
    0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
    0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038,
    0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
    0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048,
    0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
    0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058,
    0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
    0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068,
    0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
    0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078,
    0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e
    };
    private static final int EBCDIC[] = {
    0x0040, 0x005a, 0x007f, 0x007b, 0x005b, 0x006c, 0x0050, 0x007d, 0x004d,
    0x005d, 0x005c, 0x004e, 0x006b, 0x0060, 0x004b, 0x0061,
    0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8,
    0x00f9, 0x007a, 0x005e, 0x004c, 0x007e, 0x006e, 0x006f,
    0x007c, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, 0x00c8,
    0x00c9, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6,
    0x00d7, 0x00d8, 0x00d9, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
    0x00e8, 0x00e9, 0x00ad, 0x00e0, 0x00bd, 0x005f, 0x006d,
    0x0079, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088,
    0x0089, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096,
    0x0097, 0x0098, 0x0099, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
    0x00a8, 0x00a9, 0x00c0, 0x006a, 0x00d0, 0x00a1
    };

    /**
    * Description: Translates a int, either ASCII to EBCDIC or vice versa
    * @param int the int to be translated
    * @return int the translated int
    * @exception
    **/
    public final static int translateByte(int i) {
    for (int j = 0; j<SIZE; j++) {
    if (i == ASCII[j]) {
    return EBCDIC[j];
    }
    }
    return i;
    }

    private static byte readByte() throws IOException {
    int ch = input.read();
    if (ch < 0)
    throw new EOFException();
    return (byte)(ch);

    }
    private static void test() throws IOException {
    b1 = readByte();
    int t =translateByte(b1);
    output.write((int)t);

    }
    public static void main(String s[]){

    try{
    input_file = new File("input.file1");
    input = new FileInputStream(input_file);
    output_file = new File("output.file1");
    output = new FileOutputStream(output_file);
    } catch (Exception e) {
    System.out.println("Something went wrong with the files " + e);
    }
    try{
    test();
    } catch (Exception e) {
    System.out.println("Something went wrong with the files " + e);
    }
    }



    }
    Last edited by vsk; 11-14-2002 at 03:14 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here is the java code that i am using

    The first program converts comp-3 to unpacked ebcdiic format and the second one translates ebcdiic to ascii
    One more thing is that these packed fields are placed in between the normal character fields.
    How do I segregate both of them.

    Also does any one know of any c-program which does this.
    1) This is not a Java board.
    2) Use code tags, or don't post code.
    3) Don't post so much code. No one wants to read a novel just to get an idea of what you want done.
    4) Use a search engine, someone has probably already done whatever it is you want done.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. store binary data in program...
    By Abda92 in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 10:33 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21